Is there any memory issues when using timer and nested stream in flutter app

喜你入骨 提交于 2021-01-29 08:49:52

问题


I am using nested stream and a timer method for my app. Which is like a display board changes values (or screen) in every 10 seconds and repeat. I have not any quite idea about the problems in my code (it just seems working well), also thinking to add a clock in my screen, which am not sure how to implement yet (using stream again or with initstate). Please check my main code and give me some tips to clean up any issues if there. My app is using firebase to get latest data.


class OmApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'OM Fortune Branch',
      debugShowCheckedModeBanner: false,
      theme: ThemeData.dark(),
      home: MyApp(),
    );
  }
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int screen = 0;
  DocumentSnapshot docs;

//Stream for screen changing in every 10 seconds
  Stream<int> _stream() {
    Duration interval = Duration(seconds: 10);
    Stream<int> stream = Stream<int>.periodic(interval, tiktok);
    return stream;
  }

  int tiktok(int value) {
    return value;
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIOverlays([]);
    var screenSize = MediaQuery.of(context).size;
    return Scaffold(
      body: Container(
//
          child: Stack(
        children: <Widget>[
          //Settings button: Navigate to DataFeeds page
          Align(
            alignment: Alignment.bottomRight,
            child: IconButton(
                icon: Icon(
                  Icons.settings,
                  color: Colors.white70,
                  size: 25.0,
                ),
                onPressed: () {
                  if (docs != null) {
                    navigateToList();
                  }
                  print("I can't find your data in firestore");
                }),
          ),
          StreamBuilder(
              stream:
                  Firestore.instance.collection('productionrpt').snapshots(),
              //stream: strm,
              builder: (context, snapshot) {
                if (!snapshot.hasData ||
                    snapshot.connectionState == ConnectionState.waiting)
                  return const Center(child: Text('Loading...'));
                //docs = snapshot.data;
                return StreamBuilder(
                  stream: _stream(),
                  initialData: 0,
                  builder:
                      (BuildContext buildcontext, AsyncSnapshot<int> snapss) {
                    if (snapss.connectionState == ConnectionState.waiting) {
                      screen = 0;
                    }
                    if (screen > 2)
                      screen = 0;
                    else
                      screen++;
                    docs = snapshot.data.documents[screen];
                    return prodDisplay(context, docs, screenSize.width);
                  },
                );
              })
        ],
      )),
    );
  }

  void navigateToList() {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return DataFeeds();
    }));
  }
}


来源:https://stackoverflow.com/questions/60684711/is-there-any-memory-issues-when-using-timer-and-nested-stream-in-flutter-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!