Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Fix the error "The following assertion was thrown building FutureBuilder<List<Event>>(dirty, state:"

Tags:

flutter

dart

I am trying to parse JSON and convert it into list view.

I am getting the response body and it is been converted to list also but its sending null to the future builder, I am getting this error:

/flutter ( 9715): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 9715): The following assertion was thrown building FutureBuilder<List<Event>>(dirty, state:
I/flutter ( 9715): _FutureBuilderState<List<Event>>#6efa6):
I/flutter ( 9715): A build function returned null.
I/flutter ( 9715): The offending widget is: FutureBuilder<List<Event>>
I/flutter ( 9715): Build functions must never return null. To return an empty space that causes the building widget to
I/flutter ( 9715): fill available room, return "new Container()". To return an empty space that takes as little room as
I/flutter ( 9715): possible, return "new Container(width: 0.0, height: 0.0)".
I/flutter ( 9715): 
I/flutter ( 9715): When the exception was thrown, this was the stack:

The code is:

import 'package:flutter/material.dart';
import 'package:event/models/readEvent.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:event/models/global.dart';
import 'dart:async';
import 'dart:convert';
import 'package:isolate/isolate_runner.dart';

Map<String, dynamic> body = {
  "query": {"key": "XOXOXO", "value": "YOY", "specific": ""}
};


Future<List<Event>> fetchPosts(http.Client client) async {
  http.post(URL_READEVENT, body: json.encode(body)).then((response) {

print(response.body);
final parsed = json.decode(response.body);
  print(response.body);
  print(parsed['rs']);
  List<Event> event= (parsed["rs"] as List).map<Event>((json) => 
       new Event.fromJson(json)).toList();
       print(event[0].name);
  return event;

});
}


class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _count = 0;


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Events',
          style: TextStyle(color: Colors.black),
        ),
        backgroundColor: Colors.white,
      ),
      body: Container(
        child: Center(
            child: Column(children: <Widget>[
      Row(children: <Widget>[
        Container(
          padding: EdgeInsets.only(right: 20.0, left: 30.0, top: 16.0),
          child: Image.asset('lib/images/dscnew.png',
              width: 120.0, height: 120.0, fit: BoxFit.cover),
        ),
        Column(children: <Widget>[
          Text(
            "Developer Student Clubs",
            style: TextStyle(fontWeight: FontWeight.w700),
          ),
          Text(
            "VIT Vellore",
            textAlign: TextAlign.left,
            style: TextStyle(color: Colors.grey, fontWeight:FontWeight.w500),
          )
        ]),
      ]),
      new FutureBuilder(
        future: fetchPosts(new http.Client()),
        builder: (BuildContext context, AsyncSnapshot<List<Event>> snapshot) {
            if (snapshot.hasData) {
              print(snapshot.hasData);
              if (snapshot.data!=null) {
                print(snapshot.data);
                return ListViewPosts(events: snapshot.data);
              } else {
                return new CircularProgressIndicator();
              }
            }
          }
      ),

    );
  }
class ListViewPosts extends StatelessWidget {
  final List<Event> events;


  ListViewPosts({Key key, this.events}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 600,
      width: 600,
      child: Expanded(child:ListView.builder(
          itemCount: events.length,
          padding: const EdgeInsets.all(15.0),
          itemBuilder: (context, position) {
            return Column(
              children: <Widget>[
                Divider(height: 5.0),
                ListTile(
                  title: Text(
                    '${events[0].name}',
                    style: TextStyle(
                      fontSize: 22.0,
                      color: Colors.deepOrangeAccent,
                    ),
                  ),
                  subtitle: Text(
                    '${events[0].status}',
                    style: new TextStyle(
                      fontSize: 18.0,
                      fontStyle: FontStyle.italic,
                    ),
                  ),
                  leading: Column(
                    children: <Widget>[
                      CircleAvatar(
                        backgroundColor: Colors.blueAccent,
                        radius: 35.0,
                        child: Text(
                          'User ${events[0].clubName}',
                          style: TextStyle(
                            fontSize: 22.0,
                            color: Colors.white,
                          ),
                        ),
                      )
                    ],
                  ),
                  onTap: () => _onTapItem(context, events[position]),
                ),
              ],
            );
          }),
    )
    );
  }

  void _onTapItem(BuildContext context, Event post) {
    // Scaffold
    //     .of(context)
    //     .showSnackBar(new SnackBar(content: new Text(post.id.toString() + ' - ' + post.title)));
  }
}
like image 472
Prateek Mewara Avatar asked Dec 11 '22 03:12

Prateek Mewara


1 Answers

The problem is that when your FutureBuilder returns with no data (when the FutureBuilder is first loaded) you do not return a widget.

Try changing your FutureBuilder like so:

FutureBuilder(
  future: fetchPosts(new http.Client()),
  builder: (BuildContext context, AsyncSnapshot<List<Event>> snapshot) {
    if (snapshot.hasData) {
      return ListViewPosts(events: snapshot.data);
    }

    return CircularProgressIndicator();
  },
)
like image 97
Jordan Davies Avatar answered May 28 '23 07:05

Jordan Davies