Flutter - ссылка на базу данных Firebase, используемая до добавления идентификатора пользователя - PullRequest
0 голосов
/ 19 февраля 2019

Я пытаюсь получить текущий идентификатор пользователя из FirebaseAuth и затем использовать этот идентификатор как часть пути ссылки на базу данных Firebase.Я не уверен, как заставить сборку подождать, пока вернется мой текущий идентификатор пользователя, и я установил ссылку на базу данных.

С приведенным ниже кодом при первой сборке это приведет к ошибке, поскольку watchRefnull, но в конечном итоге будет работать, если в указанном местоположении есть данные.Если в указанном местоположении нет данных, слушатели никогда не setState() данные, и я просто получаю сообщение об ошибке:

Неудачное утверждение: строка 31, позиция 12: 'query! = I / flutter(11449): null ': не соответствует действительности.

Я пробовал FutureBuilders и Streams, но я что-то не так делаю.

Любая помощь будетбыть высоко ценится.

class WatchedList extends StatefulWidget {
  WatchedList({this.app, this.category});
  final FirebaseApp app;
  final String category;

  @override
  State<StatefulWidget> createState() {
    return WatchedListState();
  }
}

enum Category { watch, read, heard, done }

class WatchedListState extends State<WatchedList> {
  int count = 20;
  List<FirebaseItem> items = List();
  FirebaseItem item;
  String title = '';
  DatabaseReference watchRef;
  FirebaseUser currentUser;
  TextStyle textStyle = TextStyle(
    fontWeight: FontWeight.bold,
    fontSize: 20.0,
  );

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
    item = FirebaseItem('', '', '', '');
    _initDB();
  }

  void _initDB() async {
    final FirebaseDatabase database = FirebaseDatabase.instance;
    final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
    currentUser = await firebaseAuth.currentUser();
    switch (widget.category) {
      case 'watch':
        watchRef = database
            .reference()
            .child('users')
            .child(currentUser.uid)
            .child('watch');
        title = '...WATCHED';
        break;
      case 'read':
        watchRef = database
            .reference()
            .child('users')
            .child(currentUser.uid)
            .child('read');
        title = '...READ';
        break;
      case 'heard':
        watchRef = database
            .reference()
            .child('users')
            .child(currentUser.uid)
            .child('heard');
        title = '...HEARD';
        break;
      case 'done':
        watchRef = database
            .reference()
            .child('users')
            .child(currentUser.uid)
            .child('done');
        title = '...DONE';
        break;
    }
    watchRef.onChildAdded.listen(_onEntryAdded);
    watchRef.onChildChanged.listen(_onEntryChanged);
    print(currentUser.uid);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.filter_list),
            onPressed: () {},
          ),
          // action button
          IconButton(
            icon: Icon(Icons.settings),
            onPressed: () {},
          ),
        ],
      ),
      body: Column(
        children: <Widget>[
          Flexible(
            child: FirebaseAnimatedList(
              query: watchRef,
              itemBuilder: (BuildContext context, DataSnapshot snapshot,
                  Animation<double> animation, int index) {
                return _buildItemSlide(index);
              },
            ),
          ),
        ],
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => NewSuggestion(
                    app: widget.app,
                  ),
            ),
          );
        },
        label: Text('Add Suggestion'),
        icon: Icon(Icons.add),
      ),
    );
  }

  Widget _buildItemCard() {
    //Todo - Setting to change between cards and flat list?
    return Card(
      color: Colors.white,
      elevation: 2.0,
      child: ListTile(
        leading: CircleAvatar(
          child: Icon(Icons.ondemand_video),
        ),
        title: Text('Awesome Movie'),
        subtitle: Text('Describing awesome movie'),
        trailing: GestureDetector(
          child: Icon(
            Icons.delete,
            color: Colors.grey,
          ),
          onTap: () {},
        ),
        onTap: () {
          debugPrint('List Tile Tapped');
        },
      ),
    );
  }

  Widget _buildItemSlide(int index) {
    return ListTile(
      leading: CircleAvatar(
        child: Icon(Icons.ondemand_video),
      ),
      title: Text(items[index].title),
      subtitle: Text(items[index].body),
      trailing: GestureDetector(
        child: Icon(
          Icons.delete,
          color: Colors.grey,
        ),
        onTap: () {
          _deleteEntry(context, items[index], index);
        },
      ),
      onTap: () {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => ItemDetails(title: items[index].title)));
      },
    );
  }

  _onEntryAdded(Event event) {
    setState(() {
      items.add(FirebaseItem.fromSnapshot(event.snapshot));
    });
  }

  _onEntryChanged(Event event) {
    var old = items.singleWhere((entry) {
      return entry.key == event.snapshot.key;
    });
    setState(() {
      items[items.indexOf(old)] = FirebaseItem.fromSnapshot(event.snapshot);
    });
  }

  void _deleteEntry(
      BuildContext context, FirebaseItem item, int position) async {
    await watchRef.child(item.key).remove().then((_) {
      setState(() {
        items.removeAt(position);
      });
    });
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...