rameez khan
rameez khan

Reputation: 359

flutter A build function returned null

I am trying to use FutureBuilder but its showing error of A build function returned null

My code

class _EventsState extends State<Events> {
  @override

  Future<List> doSomeAsyncStuff() async {
    final storage = new FlutterSecureStorage();
    String value = await storage.read(key: 'token');
    print(value);

    String url = 'http://sublimeapi.netcodesolution.com/api/NewsAndEvents/';

    String token = value;
    final response = await http.get(url, headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    });
    print('Token : ${token}');
    var eventData = json.decode(response.body);
    print(eventData["Data"]);
    List _events = eventData["Data"];
    return _events;
  }

  @override
  Widget build(BuildContext context) {
    double statusBarHeight = MediaQuery
        .of(context)
        .padding
        .top;
    return Expanded(
      child: FutureBuilder(
          future: doSomeAsyncStuff(),
          builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
            List<Widget> children;
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasData) {
                print('working');
                print(snapshot.data);
                return Container(
                  child:  Column(
                      children: <Widget>[
                        Text('working')
                      ],
                    ),
                );
              }
            }
          }),
    );
  }

As you can see in code I am fetching data from API and its working fine. In code i print the value of _events in setState its also printing the value like this

I/flutter (32627): [{Id: 38, Description: Two days Workshop on Prevention of Suicide organized for GPs of Thar., ImagePath: /journals/2e8a55f3-6612-4b23-a0ea-e91022c159a8.pdf, CreatedBy: 4447, CreatedOn: 2019-09-18T14:56:13.357, Active: false, Type: Events, Attachment: null, AttachmentType: Events}

I need to print the Description value of this data in future widget but don't know why its showing error

Upvotes: 0

Views: 607

Answers (1)

Shri Hari L
Shri Hari L

Reputation: 4894

The Error says it clearly! It returned null.
So you have to return something! Do something like this,

 Future<List> doSomeAsyncStuff() async {
    final storage = new FlutterSecureStorage();
    String value = await storage.read(key: 'token');
    print(value);

    String url = 'http://sublimeapi.netcodesolution.com/api/NewsAndEvents/';

    String token = value;
    final response = await http.get(url, headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    });
    print('Token : ${token}');
    var eventData = json.decode(response.body);
    print(eventData["Data"]); 
    List _events = eventData["Data"];
    return _events; 
  }

and also, we missed another case here.

 Scaffold(
   appbar: AppBar(
      title: const Text('Sample Future Builder'),
    ),
   body: Expanded(
      child: FutureBuilder(
          future: doSomeAsyncStuff(),
          builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
             
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasData) {
                print('working');
                print(snapshot.data);
                return Container(
                  child:  Column(
                      children: <Widget>[
                        Text('working')
                      ],
                    ),
                );
              }
            }
            return Center(child: Text("Not Loaded Yet!!"),)
          }
       ),
    ),
  );

Hope that solves your issue!

Tip: Move all your widgets under Scaffold. It would be the best practice. Refer this

Upvotes: 1

Related Questions