Created
January 16, 2021 04:39
-
-
Save brendoncheung/657b10791d526b951722954e8454103d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:collection'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class Person { | |
int age = 0; | |
Person({this.age}) { | |
print("person hashcode: ${this.hashCode}"); | |
} | |
} | |
class PersonStreamController { | |
StreamController<Person> _controller = StreamController<Person>(); | |
Stream<Person> getStream() { | |
return _controller.stream; | |
} | |
void updateStream(Person person) { | |
_controller.add(person); | |
} | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
String text = "hello"; | |
var person = Person(); | |
var personStreamController = PersonStreamController(); | |
@override | |
Widget build(BuildContext context) { | |
return StreamProvider<Person>.value( | |
value: personStreamController.getStream(), | |
initialData: Person(age: 0), | |
child: Scaffold( | |
floatingActionButton: Consumer<Person>( | |
builder: (context, person, child) { | |
return FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () { | |
personStreamController.updateStream(Person(age: person.age + 1)); | |
}, | |
); | |
}, | |
), | |
appBar: AppBar( | |
title: Text("Future Provider"), | |
), | |
body: Consumer<Person>( | |
builder: (context, value, child) { | |
return Center(child: Text("Person's age: ${value.age}")); | |
}, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment