avatarAseem Wangoo

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4412

Abstract

hljs-keyword">final</span> String videoURL;

LocationModelNormal({ <span class="hljs-keyword">this</span>.name, <span class="hljs-keyword">this</span>.url, <span class="hljs-keyword">this</span>.coordinates, <span class="hljs-keyword">this</span>.videoURL, });

factory LocationModelNormal.fromMap(Map<String, <span class="hljs-keyword">dynamic</span>> <span class="hljs-keyword">data</span>) { <span class="hljs-keyword">return</span> LocationModelNormal( name: <span class="hljs-keyword">data</span>[<span class="hljs-string">'name'</span>] ?? <span class="hljs-string">''</span>, url: <span class="hljs-keyword">data</span>[<span class="hljs-string">'url'</span>] ?? <span class="hljs-string">''</span>, coordinates: <span class="hljs-keyword">data</span>[<span class="hljs-string">'location'</span>] ?? <span class="hljs-literal">null</span>, videoURL: <span class="hljs-keyword">data</span>[<span class="hljs-string">'link'</span>] ?? <span class="hljs-string">''</span>, ); }

factory LocationModelNormal.initialData() { <span class="hljs-keyword">return</span> LocationModelNormal( coordinates: <span class="hljs-literal">null</span>, name: <span class="hljs-string">''</span>, videoURL: <span class="hljs-string">''</span>, url:<span class="hljs-string">''</span>, ); } }</pre></div><p id="d9f7">2. Next, we provide the <b>initialData</b>.</p><blockquote id="f404"><p>In our case, <b>LocationModelNormal.initialData()….(See the snippet above)</b></p></blockquote><p id="d5a0">3. Finally, we provide the <b>child</b> widget.</p><blockquote id="6fbd"><p>In our case, SecondStreamWidget()…</p></blockquote><p id="4ba3" type="7">Pretty similar to StreamBuilder, right !!!!!!!!! :)</p><h2 id="436c">Access Data in Child…</h2><blockquote id="9b65"><p>Now that we have <b>StreamProvider</b> setup, we need to show the results in the child…</p></blockquote><p id="6883"><b>For accessing the data, (in your child)….</b></p><div id="ed42"><pre><span class="hljs-title">var</span> <span class="hljs-class"><span class="hljs-keyword">data</span> = <span class="hljs-type">Provider</span>.of<<span class="hljs-type">LocationModelNormal</span>>(<span class="hljs-title">context</span>);</span></pre></div><p id="ab93">Here, we specified the result expected from our stream, using <b>Provider.of<Type>(context)</b>..</p><blockquote id="e600"><p>In our case, Type = <b>LocationModelNormal</b></p></blockquote><p id="1714">In action,</p><div id="7448"><pre><span class="hljs-selector-tag">Padding</span>( <span class="hljs-attribute">padding</span>: const EdgeInsets.<span class="hljs-built_in">symmetric</span>(<span class="hljs-attribute">horizontal</span>: <span class="hljs-number">8.0</span>), <span class="hljs-attribute">child</span>: <span class="hljs-built_in">Text</span>(‘${data.name}'), ),</pre></div><p id="b571"><b>where, data = the variable we specified above…</b></p><h2 id="6873">Listen to Network Changes…</h2><figure id="f011"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*8PgITjoQ2eKLzSszjpkq6w.png"><figcaption>Flutter Provider and Streams</figcaption></figure><p id="5a51">When injecting many values in big applications, <code>Provider</code> can rapidly become pretty nested, thats why we have <code><b>MultiProvider</b></code></p><div id="ce4b"><pre><span class="hljs-selector-tag">MultiProvider</span>( <span class="hljs-attribute">providers</span>: [ Provider<Foo>.<span class="hljs-built_in">value</span>(<span class="hljs-attribute">value</span>: foo), Provider<Bar>.<span class="hljs-built_in">value</span>(<span class="hljs-attribute">value</span>: bar), Provider<Baz>.<span class="hljs-built_in">value</span>(<span class="hljs-attribute">value</span>: baz), ], <span class="hljs-attribute">child</span>: someWidget, )</pre></div><p id="1fc5">Use-Case…</p><blockquote id="3be8"><p>In our example, we listen to the network changes, before fetching other streams…..</p></blockquote><p id="8adb">How to do,</p><ol><li>Install the <a href="https://pub.dev/packages/connectivity"><b>connectivity</b></a> dependency</li><li>Wrap your <b>MaterialApp</b> with <b>MultiProvider</b></li></ol><div id="4745"><pre><span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">MultiProvider</span>( <span clas

Options

s="hljs-attribute">providers</span>: [ StreamProvider<ConnectionStatus>.<span class="hljs-built_in">value</span>( <span class="hljs-attribute">stream</span>:<span class="hljs-built_in">streamConnectivityService</span>().connectivityController.stream, ), ], <span class="hljs-attribute">child</span>: <span class="hljs-built_in">MaterialApp</span>( <span class="hljs-attribute">home</span>: <span class="hljs-built_in">Home</span>(), ), );</pre></div><p id="23d6">Here, the model class is <b>ConnectionStatus</b></p><p id="0530">Now, your Home widget will get the changes in the network connectivity…You can render different UIs accordingly…</p><p id="fc0e">Values emitted are….</p><blockquote id="5f8d"><p>Wifi, offline or mobile data….</p></blockquote><div id="6023"><pre>Widget build(BuildContext <span class="hljs-built_in">context</span>) { <span class="hljs-built_in">var</span> network = Provider.of<ConnectionStatus>(<span class="hljs-built_in">context</span>); final _width = MediaQuery.of(<span class="hljs-built_in">context</span>).size.<span class="hljs-built_in">width</span>; final _height = MediaQuery.of(<span class="hljs-built_in">context</span>).size.<span class="hljs-built_in">height</span>;

<span class="hljs-keyword">if</span> (network == ConnectionStatus.wifi ||
    network == ConnectionStatus.mobileData) {
  <span class="hljs-built_in">return</span> Container(
    child: child,
  );
}

<span class="hljs-built_in">return</span> Container(
  <span class="hljs-built_in">height</span>: _height,
  <span class="hljs-built_in">width</span>: _width,
  child: Text('YOU ARE OFFLINE'),
);

}</pre></div><p id="3b60">Articles related to Flutter:</p><div id="bab5" class="link-block"> <a href="https://readmedium.com/flutter-web-and-machine-learning-64ab1f315001"> <div> <div> <h2>Flutter Web and Machine Learning</h2> <div><h3>Using machine learning in flutter web? Check now!</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*SdPiZ5-s0F5Z3E5ENO1d8A.png)"></div> </div> </div> </a> </div><div id="7a98" class="link-block"> <a href="https://readmedium.com/provider-and-its-types-dda8463586e7"> <div> <div> <h2>Provider — and its Types</h2> <div><h3>Check out providers and its types</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*nogDwSDoTisWnX24krcadw.png)"></div> </div> </div> </a> </div><div id="8f5b" class="link-block"> <a href="https://readmedium.com/using-selector-in-provider-b32113d5da64"> <div> <div> <h2>Using Selector in Provider</h2> <div><h3>Using selector in provider</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*fpwzgIlvCRRDrr04A21ZQw.png)"></div> </div> </div> </a> </div><div id="e556" class="link-block"> <a href="https://readmedium.com/flutter-web-and-streams-e3b2d14432b6"> <div> <div> <h2>Flutter Web and Streams</h2> <div><h3>How to use streams in flutter web. Find out now!!</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*7ZZIYRl80oyw_sfk18APxA.png)"></div> </div> </div> </a> </div><p id="5e09">Src code: <a href="https://github.com/AseemWangoo/flutter_programs/blob/master/provider.zip">https://github.com/AseemWangoo/flutter_programs/blob/master/provider.zip</a></p><figure id="cf0b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rpTl0ep9orbTrbRhvOmw-w.gif"><figcaption></figcaption></figure></article></body>

Flutter Provider and Streams

Streams in, streams out…

How to listen to real-time data apart from StreamBuilder….? Hmmm…..

Using selector in Provider

All in one Flutter resource: https://flatteredwithflutter.com/how-to-use-stream-provider-in-flutter/

Begin…

Recently, a state management package called Provider was announced by Flutter team at Google I/O 2019.

How to use…

Add this dependency in your pubspec.yaml.

provider: ^2.0.0+1
Flutter Provider and Streams

Listen to data…

We had StreamBuilder in Flutter…

Hands down, best widget for listening real-time data…

This takes two arguments

  • A stream
  • A builder, that can convert the elements of the stream to widgets
StreamBuilder(
  stream: //YOUR STREAM, 
  builder: (BuildContext context, AsyncSnapshot snapshot){ 
  return //YOUR CHILD; 
})

Now we also have StreamProvider……..:)

This comes from provider package.

StreamProvider<LocationModelNormal>.value(
    initialData: LocationModelNormal.initialData(),
    stream: locationStreamInstance.specificLocation(_secondWonder),
    child: SecondStreamWidget(),
),
  1. In StreamProvider, we need to specify the type of object we are listening to….

In the above case, LocationModelNormal (model class)…

Flutter Provider and Streams

LocationModelNormal class (below)….

class LocationModelNormal {
  final String name;

  final String url;

  final GeoPoint coordinates;

  final String videoURL;

  LocationModelNormal({
    this.name,
    this.url,
    this.coordinates,
    this.videoURL,
  });

  factory LocationModelNormal.fromMap(Map<String, dynamic> data) {
    return LocationModelNormal(
      name: data['name'] ?? '',
      url: data['url'] ?? '',
      coordinates: data['location'] ?? null,
      videoURL: data['link'] ?? '',
    );
  }

  factory LocationModelNormal.initialData() {
    return LocationModelNormal(
      coordinates: null,
      name: '',
      videoURL: '',
      url:'',
    );
  }
}

2. Next, we provide the initialData.

In our case, LocationModelNormal.initialData()….(See the snippet above)

3. Finally, we provide the child widget.

In our case, SecondStreamWidget()…

Pretty similar to StreamBuilder, right !!!!!!!!! :)

Access Data in Child…

Now that we have StreamProvider setup, we need to show the results in the child…

For accessing the data, (in your child)….

var data = Provider.of<LocationModelNormal>(context);

Here, we specified the result expected from our stream, using Provider.of<Type>(context)..

In our case, Type = LocationModelNormal

In action,

Padding(
     padding: const EdgeInsets.symmetric(horizontal: 8.0),
     child: Text(‘${data.name}'),
),

where, data = the variable we specified above…

Listen to Network Changes…

Flutter Provider and Streams

When injecting many values in big applications, Provider can rapidly become pretty nested, thats why we have MultiProvider

MultiProvider(
  providers: [
    Provider<Foo>.value(value: foo),
    Provider<Bar>.value(value: bar),
    Provider<Baz>.value(value: baz),
  ],
  child: someWidget,
)

Use-Case…

In our example, we listen to the network changes, before fetching other streams…..

How to do,

  1. Install the connectivity dependency
  2. Wrap your MaterialApp with MultiProvider
return MultiProvider(
  providers: [
    StreamProvider<ConnectionStatus>.value(
           stream:streamConnectivityService().connectivityController.stream,
    ),
  ],
  child: MaterialApp(
     home: Home(),
  ),
);

Here, the model class is ConnectionStatus

Now, your Home widget will get the changes in the network connectivity…You can render different UIs accordingly…

Values emitted are….

Wifi, offline or mobile data….

Widget build(BuildContext context) {
    var network = Provider.of<ConnectionStatus>(context);
    final _width = MediaQuery.of(context).size.width;
    final _height = MediaQuery.of(context).size.height;

    if (network == ConnectionStatus.wifi ||
        network == ConnectionStatus.mobileData) {
      return Container(
        child: child,
      );
    }

    return Container(
      height: _height,
      width: _width,
      child: Text('YOU ARE OFFLINE'),
    );
}

Articles related to Flutter:

Src code: https://github.com/AseemWangoo/flutter_programs/blob/master/provider.zip

Flutter
Dart
Mobile App Development
Programming
Software Development
Recommended from ReadMedium