Created
October 2, 2019 20:41
-
-
Save fleepgeek/80c50c75c102520cff284034380b04c5 to your computer and use it in GitHub Desktop.
Corresponding code for my YouTube tutorial series for learning the BLoC pattern
This file contains hidden or 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 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.red, | |
), | |
home: FriendList(), | |
); | |
} | |
} | |
class FriendList extends StatefulWidget { | |
@override | |
_FriendListState createState() => _FriendListState(); | |
} | |
class _FriendListState extends State<FriendList> { | |
List<String> _friends = []; | |
final _friendsController = StreamController<List<String>>(); | |
final _textController = TextEditingController(); | |
@override | |
void dispose() { | |
_friendsController.close(); | |
_textController.dispose(); | |
super.dispose(); | |
} | |
void addFriend(String value) { | |
_friends.add(value); | |
_friendsController.sink.add(_friends); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Friend List"), | |
), | |
body: Column( | |
children: <Widget>[ | |
Expanded( | |
child: _buildFriendList(), | |
), | |
Padding( | |
padding: EdgeInsets.all(8.0), | |
child: TextField( | |
controller: _textController, | |
decoration: InputDecoration( | |
border: OutlineInputBorder(), | |
hintText: 'Enter New Friend', | |
), | |
onSubmitted: (value) { | |
addFriend(value); | |
_textController.clear(); | |
}, | |
), | |
), | |
], | |
), | |
); | |
} | |
Widget _buildFriendList() { | |
return StreamBuilder<List<String>>( | |
stream: _friendsController.stream, | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
return ListView.separated( | |
itemCount: _friends.length, | |
separatorBuilder: (context, index) => Divider(), | |
itemBuilder: (context, index) { | |
return ListTile( | |
title: Text('${snapshot.data[index]}'), | |
); | |
}, | |
); | |
} | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Icon(Icons.add), | |
Text('Add New Friend'), | |
], | |
); | |
}); | |
} | |
} |
This file contains hidden or 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:convert'; | |
import 'dart:io'; | |
void main() { | |
//streamFromIterable(); | |
//streamUsingController(); | |
// readNameFile(); | |
} | |
void streamFromIterable() { | |
final list = [2, 3, 8, 9, 12]; | |
final stream = Stream.fromIterable(list); | |
final subscription = stream.where((value) => value.isEven).listen(null); | |
subscription.onData((value) => print(value)); | |
Another way | |
subscription.onData((value){ | |
if(value == 9){ | |
subscription.cancel(); | |
} | |
print(value); | |
}); | |
subscription.onError((err) => print('Error $err')); | |
subscription.onDone(() => print('Done')); | |
} | |
void streamUsingController() { | |
final _controller = StreamController<int>.broadcast(); | |
Stream<int> counterStream = _controller.stream; | |
final sub1 = counterStream.listen((value) => print(value)); | |
final sub2 = counterStream.listen((value) => print(value)); | |
_controller.sink.add(1); | |
Future.delayed(Duration(milliseconds: 2000), () { | |
_controller.sink.add(2); | |
_controller.close(); | |
}); | |
//_controller.close(); | |
} | |
void readNameFile() { | |
final file = File('names.txt'); | |
file.openRead() | |
.transform(utf8.decoder) | |
.transform(customTransformer) | |
.transform(LineSplitter()) | |
.listen((value){ | |
print("Received"); | |
print(value); | |
}); | |
} | |
var customTransformer = StreamTransformer<String, String>.fromHandlers( | |
handleData: (value, sink) { | |
sink.add(value.toUpperCase()); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment