Nob Bahter
Nob Bahter

Reputation: 25

Flutter Build function must never be null

I'm trying return a widget where it returns a container with a text that says 'Cannot find device' if it doesn't find that specific device and if it does find it, the text should say 'Device found!'. I'm new to flutter and tried to play around with the Flutter Blue package to get a grasp of the framework while working on a project. This is my original code:

 Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Find Devices'),
      ),
      body: RefreshIndicator(
        onRefresh: () =>
            FlutterBlue.instance.startScan(timeout: Duration(seconds: 4)),
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              StreamBuilder<List<ScanResult>>(
                  stream: FlutterBlue.instance.scanResults,
                  initialData: [],
                  builder: (c, snapshot) {
                    child:
                    snapshot.data.map((r) => () {
                          if (r.device.id.toString() == '30:AE:A4:1A:B1:4E') {
                            return Container(
                              child: Text('Device found!'),
                            );
                          } else {
                            return Container(
                              child: Text('Cannot find device'),
                            );
                          }
                        }); //endof map
                  }),
            ],
          ),
        ),
      ),
      floatingActionButton: StreamBuilder<bool>(
        stream: FlutterBlue.instance.isScanning,
        initialData: false,
        builder: (c, snapshot) {
          if (snapshot.data) {
            return FloatingActionButton(
              child: Icon(Icons.stop),
              onPressed: () => FlutterBlue.instance.stopScan(),
              backgroundColor: Colors.red,
            );
          } else {
            return FloatingActionButton(
                child: Icon(Icons.search),
                onPressed: () => FlutterBlue.instance
                    .startScan(timeout: Duration(seconds: 4)));
          }
        },
      ),
    );
  }

Meanwhile I tried making a widget but I'm not sure if I did it right because I still have the same error.

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Find Devices'),
      ),
      body: RefreshIndicator(
        onRefresh: () =>
            FlutterBlue.instance.startScan(timeout: Duration(seconds: 4)),
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              StreamBuilder<List<ScanResult>>(
                  stream: FlutterBlue.instance.scanResults,
                  initialData: [],
                  builder: (c, snapshot) {
                    child:
                    snapshot.data.map((r) => () {
                          return _buildResult(r);
                        }); //endof map
                  }),
            ],
          ),
        ),
      ),
      floatingActionButton: StreamBuilder<bool>(
        stream: FlutterBlue.instance.isScanning,
        initialData: false,
        builder: (c, snapshot) {
          if (snapshot.data) {
            return FloatingActionButton(
              child: Icon(Icons.stop),
              onPressed: () => FlutterBlue.instance.stopScan(),
              backgroundColor: Colors.red,
            );
          } else {
            return FloatingActionButton(
                child: Icon(Icons.search),
                onPressed: () => FlutterBlue.instance
                    .startScan(timeout: Duration(seconds: 4)));
          }
        },
      ),
    );
  }

  _buildResult(result) {
    if (result.device.id.toString() == mydevice) {
      return Container(
        child: Text('Device found!'),
      );
    } else {
      return Container(
        child: Text('Cannot find device'),
      );
    }
  }

Upvotes: 0

Views: 76

Answers (2)

Abdul Malik
Abdul Malik

Reputation: 1188

in the builder function of your StreamBuilder, why are you using the child parameter?

builder: (c, snapshot) { child: snapshot.data.map((r) => () { return _buildResult(r); }); //endof map }),

Try this once

builder: (c, snapshot) { return snapshot.data.map((r) => () { return _buildResult(r); }); //endof map }),

Also since you are using the .map() method there may be a posibility of having more than 1 widget returned, so its better to use a listview in my opinion.

Upvotes: 1

EdwynZN
EdwynZN

Reputation: 5601

you forgot the return inside the Steam builder

StreamBuilder<List<ScanResult>>(
   stream: FlutterBlue.instance.scanResults,
   initialData: [],
   builder: (c, snapshot) {
   //child: I don't know what this (child:) parameter is doing here, 
   //there is no child inside the builder
     if(snapshot.data.length > 0) //check if there is data to map,
                                  // initialData is an empty List so it won't map anything
       return snapshot.data.map((r) => () { //You forgot the return here
         return _buildResult(r);
       }); //endof map
     else return SizedBox();
}),

Upvotes: 1

Related Questions