栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Flutter中使用流/接收器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Flutter中使用流/接收器

非常感谢vbandrade,他的回答帮助我弄清楚了。与我合作的解决方案是:

StreamController
如果需要
sink
在我的
bloc
业务逻辑组件中侦听a ,然后
stream
将其输出到其他元素,则需要2 。

counter_bloc.dart
是:

import 'dart:async';class CounterBloc {  int _count = 0;  // The controller to stream the final output to the required StreamBuilder  final _counter = StreamController.broadcast<int>();  Stream<int> get counter => _counter.stream;  // The controller to receive the input form the app elements       final _query = StreamController<int>();  Sink<int> get query => _query.sink;  Stream<int> get result => _query.stream;  // The business logic  CounterBloc() {    result.listen((increment) {     // Listen for incoming input    _count += increment;          // Process the required data      _counter.add(_count);         // Stream the required output    });  }  void dispose(){    _query.close();    _counter.close();  }}

并且

main.dart
是:

import 'counter_bloc.dart';import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      title: 'Flutter Demo',      theme: ThemeData(        primarySwatch: Colors.blue,      ),      home: MyHomePage(title: 'Flutter Demo Home Page'),    );  }}class MyHomePage extends StatefulWidget {  MyHomePage({Key key, this.title}) : super(key: key);  final String title;  @override  State<StatefulWidget> createState() {    return _MyHomePageState();  }}class _MyHomePageState extends State<MyHomePage> {  var bloc = CounterBloc();  @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: Text(widget.title),      ),      body: Center(        child: Column(          mainAxisAlignment: MainAxisAlignment.center,          children: <Widget>[ Text(   'You have pushed the button this many times:', ), StreamBuilder<int>(      // Listen to the final output sent from the Bloc   stream: bloc.counter,   initialdata: 0,   builder: (BuildContext c, AsyncSnapshot<int> data) {     return Text(       '${data.data}',       style: Theme.of(context).textTheme.display1,     );   }, ),          ],        ),      ),      floatingActionButton: Row(        mainAxisAlignment: MainAxisAlignment.end,        children: <Widget>[          FloatingActionButton( onPressed: () {   bloc.query.add(2);         // Send input to the Bloc }, tooltip: 'Increment 2', child: Text("+2"),          ),          FloatingActionButton( onPressed: () {   bloc.query.add(1);        // Send input to the Bloc }, tooltip: 'Increment 1', child: Text("+1"),          ),        ],      ), // This trailing comma makes auto-formatting nicer for build methods.    );  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427773.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号