text stringlengths 1 372 |
|---|
why? in declarative frameworks like flutter, if you want to change the UI, |
you have to rebuild it. there is no easy way to have |
MyCart.updateWith(somethingNew). in other words, it’s hard to |
imperatively change a widget from outside, by calling a method on it. |
and even if you could make this work, you would be fighting the |
framework instead of letting it help you. |
even if you get the above code to work, |
you would then have to deal |
with the following in the MyCart widget: |
you would need to take into consideration the current state of the UI |
and apply the new data to it. it’s hard to avoid bugs this way. |
in flutter, you construct a new widget every time its contents change. |
instead of MyCart.updateWith(somethingNew) (a method call) |
you use MyCart(contents) (a constructor). because you can only |
construct new widgets in the build methods of their parents, |
if you want to change contents, it needs to live in MyCart’s |
parent or above. |
<code_start> |
// GOOD |
void myTapHandler(BuildContext context) { |
var cartModel = somehowGetMyCartModel(context); |
cartModel.add(item); |
} |
<code_end> |
now MyCart has only one code path for building any version of the UI. |
<code_start> |
// GOOD |
widget build(BuildContext context) { |
var cartModel = somehowGetMyCartModel(context); |
return SomeWidget( |
// just construct the UI once, using the current state of the cart. |
// ··· |
); |
} |
<code_end> |
in our example, contents needs to live in MyApp. whenever it changes, |
it rebuilds MyCart from above (more on that later). because of this, |
MyCart doesn’t need to worry about lifecycle—it just declares |
what to show for any given contents. when that changes, the old |
MyCart widget disappears and is completely replaced by the new one. |
this is what we mean when we say that widgets are immutable. |
they don’t change—they get replaced. |
now that we know where to put the state of the cart, let’s see how |
to access it. |
<topic_end> |
<topic_start> |
accessing the state |
when a user clicks on one of the items in the catalog, |
it’s added to the cart. but since the cart lives above MyListItem, |
how do we do that? |
a simple option is to provide a callback that MyListItem can call |
when it is clicked. dart’s functions are first class objects, |
so you can pass them around any way you want. so, inside |
MyCatalog you can define the following: |
<code_start> |
@override |
widget build(BuildContext context) { |
return SomeWidget( |
// construct the widget, passing it a reference to the method above. |
MyListItem(myTapCallback), |
); |
} |
void myTapCallback(Item item) { |
print('user tapped on $item'); |
} |
<code_end> |
this works okay, but for an app state that you need to modify from |
many different places, you’d have to pass around a lot of |
callbacks—which gets old pretty quickly. |
fortunately, flutter has mechanisms for widgets to provide data and |
services to their descendants (in other words, not just their children, |
but any widgets below them). as you would expect from flutter, |
where everything is a widget™, these mechanisms are just special |
kinds of widgets—InheritedWidget, InheritedNotifier, |
InheritedModel, and more. we won’t be covering those here, |
because they are a bit low-level for what we’re trying to do. |
instead, we are going to use a package that works with the low-level |
widgets but is simple to use. it’s called provider. |
before working with provider, |
don’t forget to add the dependency on it to your pubspec.yaml. |
to add the provider package as a dependency, run flutter pub add: |
now you can import 'package:provider/provider.dart'; |
and start building. |
with provider, you don’t need to worry about callbacks or |
InheritedWidgets. but you do need to understand 3 concepts: |
<topic_end> |
<topic_start> |
ChangeNotifier |
ChangeNotifier is a simple class included in the flutter SDK which provides |
change notification to its listeners. in other words, if something is |
a ChangeNotifier, you can subscribe to its changes. (it is a form of |
observable, for those familiar with the term.) |
in provider, ChangeNotifier is one way to encapsulate your application |
state. for very simple apps, you get by with a single ChangeNotifier. |
in complex ones, you’ll have several models, and therefore several |
ChangeNotifiers. (you don’t need to use ChangeNotifier with provider |
at all, but it’s an easy class to work with.) |
in our shopping app example, we want to manage the state of the cart in a |
ChangeNotifier. we create a new class that extends it, like so: |
<code_start> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.