0

I have the below FutureBuilder entry.

FutureBuilder(
                future: _checkConn,
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  switch (snapshot.connectionState){
                    case ConnectionState.none:
                      Navigator.pushReplacementNamed(context, NoConnView);
                      break;
                    case ConnectionState.active:
                    case ConnectionState.waiting:
                    case ConnectionState.done:
                      if(snapshot.data=='OK'){
                        initialfbget();
                        break;
                      } else {
                        Navigator.pushReplacementNamed(context, NoConnView);
                        break;
                      }
                  }
                  return SizedBox.expand(
                    child: FittedBox(
                      fit: BoxFit.fill,
                      child: SizedBox(
                        width: _vcontroller.value.size?.width ?? (MediaQuery.of(context).size.width),
                        height: _vcontroller.value.size?.height ?? (MediaQuery.of(context).size.height),
                        child: VideoPlayer(_vcontroller),
                      ),
                    ),
                  );

                }
            ),

The below is the complete initstate section:

void initState()  {
    super.initState ();

    _vcontroller = VideoPlayerController.asset("assets/testimages/sukiitestanimation.mp4")
      ..initialize().then((_) {
        // Once the video has been loaded we play the video and set looping to true.
        _vcontroller.play();
        _vcontroller.setLooping(false);
        // Ensure the first frame is shown after the video is initialized.
      });

    _checkConn = checkConn();
    Firebase.initializeApp();
  }

Below is the checkconn segment:

Future<String> checkConn() async {
    var connresponse = await http.get(connurl).timeout(const Duration(seconds: 10));
    log('connresponse is: ${connresponse.statusCode}');
    if(connresponse.statusCode!=200) {
      return "BAD";
    } else {
      return "OK";
    }
  }

Kept on receiving the below error.

setState() or markNeedsBuild() called during build.

Would appreciate any assistance on this.

Thanks in advance.

  • can you share your `checkConn()` and your full `initState()` method? it will help another volunteer to answer this – Gilang Pratama Jan 17 '21 at 00:29
  • ```Future checkConn() async { var connresponse = await http.get(connurl).timeout(const Duration(seconds: 10)); log('connresponse is: ${connresponse.statusCode}'); if(connresponse.statusCode!=200) { return "BAD"; } else { return "OK"; } } – marcusaureliusbrutus Jan 17 '21 at 00:30
  • @GilangPratama, re-pasting requested info. `Future checkConn() async { var connresponse = await http.get(connurl).timeout(const Duration(seconds: 10)); log('connresponse is: ${connresponse.statusCode}'); if(connresponse.statusCode!=200) { return "BAD"; } else { return "OK"; } }`. – marcusaureliusbrutus Jan 17 '21 at 00:35
  • `void initState() { super.initState (); _vcontroller = VideoPlayerController.asset("assets/testimages/sukiitestanimation.mp4") ..initialize().then((_) { // Once the video has been loaded we play the video and set looping to true. _vcontroller.play(); _vcontroller.setLooping(false); // Ensure the first frame is shown after the video is initialized. }); _checkConn = checkConn(); Firebase.initializeApp(); }` – marcusaureliusbrutus Jan 17 '21 at 00:35

1 Answers1

0

Actually, I don't know what is _checkConn = checkConn(); in your initState() used for, because you've declare your checkConn() at your futureBuilder. I just can suggest you to solve your problem with these 3 option that you can pick bellow:

1. Remove your await in initState

just delete your _checkConn = checkConn();

2. Remove your setState() during your build Widget

from my experience, the error like I write below can happen when you call setState() during the building widget.

setState() or markNeedsBuild() called during build.

so, I assume that you have some setState() inside your build without any event.

3. Use if mounted

await syntax can make some error when you build it in initState(), you can use 1 of some StatefulWidget() lifecycle to build it, that is mounted.

Gilang Pratama
  • 439
  • 6
  • 18