Tempelritter
Tempelritter

Reputation: 543

Flutter & Firebase: Error with FutureBilder

Currently i develop a Meal and Shopping App. In this App you can Add what you want to Eat next and have the secound Tab, Shopping where you can Add your Items you want to buy next. Created is that a User can invite another User to edit together the List.

I get the Error shown below. I can't figure out how to return the Container. At the void saveInviteToFirestore the user is not used do I need that it used?

Code

import 'package:flutter/material.dart';
import 'package:mealapp/models/Widgets/whenAndWhatToEat.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:intl/intl.dart';
import 'package:mealapp/models/global.dart';
import 'package:status_alert/status_alert.dart';
import 'package:firebase_auth/firebase_auth.dart';

class MealTile extends StatefulWidget {
  final MealsAndWhen mealsAndWhen;
  MealTile({this.mealsAndWhen});

  @override
  MealTileState createState() {
    return MealTileState();
  }
}

class MealTileState extends State<MealTile> {
  String id;
  final db = Firestore.instance;
  String mail;
  List<String> authors = [];

  DateTime selectedDate = DateTime.now();

  Future pickDate() async {
    DateTime datepick = await showDatePicker(
        context: context,
        initialDate: new DateTime.now(),
        firstDate: new DateTime.now().add(Duration(days: -0)),
        lastDate: new DateTime.now().add(Duration(days: 365)));
    if (datepick != null)
      setState(() {
        selectedDate = datepick;
      });
  }

  Future<String> inputData() async {
    final FirebaseUser user = await FirebaseAuth.instance.currentUser();
    return user != null ? user.uid : null;
  }

  Future<String> inputDataMail() async {
    final FirebaseUser user = await FirebaseAuth.instance.currentUser();
    return user != null ? user.email : null;
  }

  String userId;

  void _getUserId() {
    inputData().then((value) => setState(() {
          userId = value;
        }));
  }

  String currentMail;

  void _getMail(doc) {
    inputDataMail().then((value) => setState(() {
          currentMail = value;
        }));
  }

  /*void _getAuthors(DocumentSnapshot doc) async {
    authors = [];
    //if (await FirebaseAuth.instance.currentUser() != null) {
      authors = List.from(doc.data['Authors']);
      print(doc.data['authors']);
      //authors.insert(0, currentMail);
    //}
  }*/

  Widget buildItem(DocumentSnapshot doc) {
    DateTime now = doc.data['Date'].toDate();
    DateFormat formatter = DateFormat('dd-MM-yyyy');
    String formatted = formatter.format(now);
    _getUserId();
    _getMail(doc);
    if (doc.data['Authors'] != null) {
      //_getAuthors(doc);
      //print('Current mail: ' + currentMail + authors.toString() + doc.data['Author'] + doc.data['Meal']);
    }

    if (now.day == DateTime.now().day) { // If the Date of the meal is today
      deleteData(doc, false); // Delete it!
    }
    // You could also change ".day" to ".hour".
    // Example: if (now.day == DateTime.now().day && now.hour == DateTime.hour())
    // So, if a meal is set for 2PM, it will delete at 2PM

    return FutureBuilder<FirebaseUser>(
        future: FirebaseAuth.instance.currentUser(),
        builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
          if (snapshot.hasData && snapshot != null) {
            return Container(
              margin: const EdgeInsets.all(8.0),
              child: currentMail == doc.data['Author'] || // If the current mail is the author
                      List.from(doc.data['Authors']).contains(currentMail) // Or if the current mail is part of the authors
                  ? Column( // then if  true, show a Column
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Text(
                          'Meal:',
                          style: TextStyle(
                              fontSize: 24,
                              fontWeight: FontWeight.bold,
                              color: Colors.white),
                          textAlign: TextAlign.center,
                        ),
                        Text(
                          '${doc.data['Meal']}',
                          style: TextStyle(
                              fontSize: 24,
                              fontWeight: FontWeight.bold,
                              color: Colors.white),
                          textAlign: TextAlign.center,
                        ),
                        SizedBox(height: 20),
                        Text(
                          'When:',
                          style: TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                              color: Colors.white),
                          textAlign: TextAlign.center,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            IconButton(
                              onPressed: () => updateData(doc),
                              color: lightBlueColor,
                              icon: Icon(Icons.calendar_today,
                                  color: Colors.white),
                              tooltip: 'Update Date',
                            ),
                            Text(
                              formatted,
                              style: TextStyle(
                                  fontSize: 20,
                                  fontWeight: FontWeight.bold,
                                  color: Colors.white),
                              textAlign: TextAlign.center,
                            ),
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: <Widget>[
                            SizedBox(width: 8),
                            FlatButton(
                              color: Colors.red,
                              onPressed: () => deleteData(doc, true),
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                      BorderRadiusDirectional.circular(12)),
                              child: Row(children: <Widget>[
                                Text('Delete',
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        color: Colors.white)),
                                Icon(Icons.delete_forever, color: Colors.white),
                              ]),
                            ),
                            SizedBox(width: 8),
                            FlatButton(
                              color: Colors.blue,
                              onPressed: () => [
                                showDialog(
                                    context: context,
                                    builder: (BuildContext context) {
                                      return Dialog(
                                        child: invite(doc),
                                        shape: RoundedRectangleBorder(
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(12)),
                                        ),
                                      );
                                    })
                              ],
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                      BorderRadiusDirectional.circular(12)),
                              child: Row(children: <Widget>[
                                Text('Invite',
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        color: Colors.white)),
                                Icon(Icons.share, color: Colors.white),
                              ]),
                            ),
                          ],
                        ),
                      ],
                    )
                  : Text(''), // if false, show an empty text widget
              decoration: BoxDecoration(
                color: lightBlueColor,
                borderRadius: BorderRadius.all(Radius.circular(12)),
              ),
            );
          }
          /*Navigator.pop(context);
          return HomePage();*/
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: darkGreyColor,
      body: ListView(
        padding: EdgeInsets.only(top: 220),
        children: <Widget>[
          StreamBuilder<QuerySnapshot>(
            stream: db
                .collection('mealList')
                .orderBy('Date', descending: false) // Order by Date, not descending
                .snapshots(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Column(
                    children: snapshot.data.documents
                        .map((doc) => buildItem(doc))
                        .toList());
              } else {
                return Container();
              }
            },
          ),
        ],
      ),
    );
  }

  /*share(BuildContext context, DocumentSnapshot doc) {
    final RenderBox box = context.findRenderObject();
    final dynamic date = timeago.format(doc['Date'].toDate());

    Share.share(
      "${doc['Meal']} - $date",
      subject: doc['Meal'],
      sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size,
    );
  }*/

  Widget invite(DocumentSnapshot doc) {
    final _formKey = GlobalKey<FormState>();
    return Form(
      key: _formKey,
      child: Padding(
        padding: const EdgeInsets.all(24.0),
        child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
          Center(
              child: Text(
            "Invite someone by mail",
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
          )),
          SizedBox(
            height: 24,
          ),
          TextFormField(
            decoration: InputDecoration(
                border: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(12))),
                labelText: 'Enter the email address'),
            validator: (value) {
              if (value.isEmpty) {
                return 'Please enter an email address';
              }
              return null;
            },
            onSaved: (value) => mail = value,
          ),
          FlatButton(
            onPressed: () async {
              if (_formKey.currentState.validate()) {
                _formKey.currentState.save();
                saveInviteToFirestore(doc, mail);
              }
            },
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(12))),
            child: Text("Save"),
            color: redColor,
            textColor: Colors.white,
          ),
        ]),
      ),
    );
  }

  Future<String> getCurrentUser() async {
    return await FirebaseAuth.instance.currentUser().then((value) => value.uid);
  }

  void saveInviteToFirestore(DocumentSnapshot doc, String email) async {
    final String user = await getCurrentUser();
    var list = List<String>();
    list.add(email);

    Firestore.instance
        .collection('mealList')
        .document(doc.documentID)
        .updateData({"Authors": FieldValue.arrayUnion(list)});
    //setState(() => id = doc.documentID);
    StatusAlert.show(
      context,
      duration: Duration(seconds: 2),
      title: 'Added',
      subtitle: 'You have Added your and the Date to your List',
      configuration: IconConfiguration(icon: Icons.done),
    );
    //Navigator.pop(context);
  }

  void deleteData(DocumentSnapshot doc, bool showMessage) async {
    await db.collection('mealList').document(doc.documentID).delete();
    setState(() => id = null);
    if (showMessage) {
      StatusAlert.show(
        context,
        duration: Duration(seconds: 2),
        title: 'Deleted',
        subtitle: 'You have Deleted your Meal',
        configuration: IconConfiguration(icon: Icons.delete),
      );
    }
  }

  void updateData(DocumentSnapshot doc) async {
    await pickDate();
    await db
        .collection('mealList')
        .document(doc.documentID)
        .updateData({'Date': selectedDate});
    StatusAlert.show(
      context,
      duration: Duration(seconds: 2),
      title: 'Updated',
      subtitle: 'You have updated your Meal Date',
      configuration: IconConfiguration(icon: Icons.done),
    );
  }
}


Error

The following assertion was thrown building FutureBuilder<FirebaseUser>(dirty, state: _FutureBuilderState<FirebaseUser>#a4504):
A build function returned null.

The offending widget is: FutureBuilder<FirebaseUser>
Build functions must never return null.

To return an empty space that causes the building widget to fill available room, return "Container()". To return an empty space that takes as little room as possible, return "Container(width: 0.0, height: 0.0)".

The relevant error-causing widget was
    FutureBuilder<FirebaseUser> 
lib/…/MealPlan/mealTile.dart:92
When the exception was thrown, this was the stack
#0      debugWidgetBuilderValue.<anonymous closure> 
package:flutter/…/widgets/debug.dart:276

Upvotes: 0

Views: 131

Answers (1)

Christopher Moore
Christopher Moore

Reputation: 17123

In your FutureBuilder you are not returning anything when the Future hasn't completed yet. A widget always needs to be returned whether there is data or not.

Example fix for your code:

return FutureBuilder<FirebaseUser>(
        future: FirebaseAuth.instance.currentUser(),
        builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
          if (snapshot.hasData && snapshot != null) {
            return Container(
             ...
            );
          }
          //ADDED ELSE BLOCK
          else {
            return Container();
          }
        }
      );

Or as @stacker suggested, you can return a CircularProgressIndicator().

Upvotes: 2

Related Questions