【问题标题】:what should be passed in that " stream: " section? [closed]应该在那个“流:”部分中传递什么? [关闭]
【发布时间】:2019-08-31 19:21:23
【问题描述】:

【问题讨论】:

    标签: flutter dart provider


    【解决方案1】:

    您之前创建的 Stream 函数。

    工作示例代码(在本例中为UsingStreamBuilder

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    
    class UsingStreamBuilder extends StatelessWidget {
      Stream<int> timedCounter(Duration interval, [int maxCount]) async* {
        int i = 0;
        while (true) {
          await Future.delayed(interval);
          yield i++;
          if (i == maxCount) break;
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text("StreamBuilder in Flutter")),
          body: Center(
            child: StreamBuilder<int>(
              stream: timedCounter(Duration(seconds: 2), 10),
              //print an integer every 2secs, 10 times
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return Text("No data");
                }
                return Text("${snapshot.data.toString()}",
                    style: TextStyle(fontSize: 20));
              },
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2013-07-26
      • 2013-05-09
      相关资源
      最近更新 更多