2

[对不起我的英语不好]

在这个项目中,我正在查看来自JSON API的某些页面中的事件列表,在 CloudEvents页面中,我正在查看来自Firebase Database的另一个列表,使用事件模型......

现在,当我运行CloudEvents页面时,我得到 nullitemsEvent[i].startDate

出现此错误:A non-null String must be provided to a Text widget.

打开:错误快照

打开:Firebase 数据库快照

我将此方法用于我的模型

这是我的事件模型

    class Event {
          String key;
          String sectionID;
          String sectionTitle;
          String title;
          String startDate;
          String endDate;
          String startDateAr;
          String endDateAr;
          String city;
          String detail;
          String location;
          String more;
          String link;

          // for generalevent.json
          String eventDate;
          String eventDateAr;

          Event(
              {this.sectionID,
              this.sectionTitle,
              this.title,
              this.startDate,
              this.endDate,
              this.startDateAr,
              this.endDateAr,
              this.city,
              this.detail,
              this.location,
              this.more,
              this.link,
              this.eventDate,
              this.eventDateAr});

          factory Event.fromJson(Map<String, dynamic> json) {
            return Event(
              sectionID: json['section-id'],
              sectionTitle: json['section-title'],
              title: json['title'],
              startDate: json['start-event-date'],
              endDate: json['end-event-date'],
              startDateAr: json['start-event-date-ar'],
              endDateAr: json['end-event-date-ar'],
              city: json['city'],
              detail: json['detail'],
              location: json['location'],
              more: json['more'],
              link: json['link'],

              // for generalevent.json
              eventDate: json['event-date'],
              eventDateAr: json['event-date-ar'],
            );
          }

          Event.fromSnapshot(DataSnapshot snapshot)
              : key = snapshot.key,
                title = snapshot.value['title'],
                startDate = snapshot.value['startDate'];

          toJson() {
            return {
              "title": title,
              "startDate": startDate,
            };
          }

        }

这是我的CloudEvents页面

    import 'package:events/UI/styleguide.dart';
    import 'package:events/models/event.dart' as e;
    import 'package:events/pages/account.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:firebase_database/firebase_database.dart';
    import 'package:firebase_database/ui/firebase_animated_list.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/rendering.dart';
    import 'package:intl/intl.dart' as intl;

    final FirebaseAuth mAuth = FirebaseAuth.instance;
    final db = FirebaseDatabase.instance.reference();
    FirebaseUser mCurrentUser;

    List<e.Event> itemsEvent = List();
    e.Event itemEvent;
    DatabaseReference dbEvent;



    class CloudEvents extends StatefulWidget {
      final GlobalKey<ScaffoldState> scaffoldKey;

      const CloudEvents({Key key, @required this.scaffoldKey}) : super(key: key);

      @override
      _CloudEventsState createState() => _CloudEventsState();
    }

    class _CloudEventsState extends State<CloudEvents> {
      ScrollController _hideButtonController;
      var _isVisible;

      @override
      initState() {
        super.initState();

        ...
        
        itemEvent = e.Event();
        final FirebaseDatabase database = FirebaseDatabase.instance;
        dbEvent = database.reference().child('events');
        dbEvent.onChildAdded.listen(_onEntryAddedEvent);
        dbEvent.onChildChanged.listen(_onEntryChangedEvent);

        ...
      }

      _onEntryAddedEvent(Event event) {
        setState(() {
          itemsEvent.add(e.Event.fromSnapshot(event.snapshot));
        });
      }

      _onEntryChangedEvent(Event event) {
        var old = itemsEvent.singleWhere((entry) {
          return entry.key == event.snapshot.key;
        });
        setState(() {
          itemsEvent[e.Event.indexOf(old)] = e.Event.fromSnapshot(event.snapshot);
        });
      }

      @override
      Widget build(BuildContext context) {
        return showEvents();
      }

      Widget showEvents(BuildContext context) {

        return Container(
          height: MediaQuery.of(context).size.height,
          child: Stack(
            children: <Widget>[
              FirebaseAnimatedList(
                  query: dbEvent.child(mCurrentUser.uid),
                  itemBuilder: (_, DataSnapshot snapshot,
                      Animation<double> animation, int i) {
                    return new ListTile(
                      title: new Text(snapshot.value['title']),  //snapshot works well 
                      subtitle: new Text(itemsEvent[i].startDate), // this returns null
                    );
                  }),

              ...

            ],
          ),
        );
      }

      ...

    }
4

1 回答 1

3

根据u/Cholojuanito对 reddit的评论,我刚刚意识到这里的问题

我做了他的第 1 个可能的解决方案,然后我编辑:

Text(itemsEvent[i].startDate)Text(e.Event.fromSnapshot(snapshot).startDate)

效果很好,感谢所有对 reddit 发表评论的人。

于 2019-09-20T16:41:27.403 回答