Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

google cloud firestore - ERROR:flutter/lib/ui/ui_dart_state.cc(177) Unhandled Exception: NoSuchMethodError: The method 'forEach' was called on null

I first cleared all the cache records. When I executed my APP for the first login, the code popped up an error saying that foreach mothod is equal to null, but I can read the data when I jump to other pages. I can’t understand the reason. What can I do to solve this problam?

this is my code

path reference

advanced() async {
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseUser currentUser = await _auth.currentUser();
final db = Firestore.instance;
(new Offline(currentUser.uid)).getData().then((data) {  //call data at this line
  var vip = new Vip(currentUser.uid);
  vip.isVip().then((vipStatus) {
    if (vipStatus) {
      setState(() {
        _advancedWidget = Container(
          child: Center(
            child: Container(
              padding: const EdgeInsets.only(left: 8.0, top: 8),
              child: Text(
                'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
                style: AppTheme.getTextStyle(themeData.textTheme.subtitle1,
                    fontWeight: FontWeight.w500),
                maxLines: 5,
              ),
            ),
          ),
        );
      });
      for (var course in data['course']) {
        if (course['start'].millisecondsSinceEpoch <=
                DateTime.now().millisecondsSinceEpoch &&
            DateTime(
                        DateTime.fromMillisecondsSinceEpoch(
                                course['start'].millisecondsSinceEpoch)
                            .year,
                        DateTime.fromMillisecondsSinceEpoch(
                                course['start'].millisecondsSinceEpoch)
                            .month,
                        DateTime.fromMillisecondsSinceEpoch(
                                    course['start'].millisecondsSinceEpoch)
                                .day +
                            course['period'] -
                            1,
                        23,
                        59,
                        00)
                    .millisecondsSinceEpoch >=
                DateTime.now().millisecondsSinceEpoch) {
          // 期間內
          final dayFromRegister = ((DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day).millisecondsSinceEpoch -
                      course['start'].millisecondsSinceEpoch) /
                  1000 /
                  60 /
                  60 /
                  24)
              .round();
          _advancedList.clear();
          for (var i = 0; i < 28; i++) {
            DateTime date = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day)
                .add(new Duration(days: (dayFromRegister - i) * -1));
            List<dynamic> result = data['records'].reversed.toList();
            int completion = 0;
            for (var r in result) {
              if (r['year'] == date.year &&
                  r['month'] == date.month &&
                  r['day'] == date.day &&
                  r['time_period'] == 'am') {
                completion += 1;
                break;
              }
            }
            for (var r in result) {
              if (r['year'] == date.year &&
                  r['month'] == date.month &&
                  r['day'] == date.day &&
                  r['time_period'] == 'pm') {
                completion += 1;
                break;
              }
            }
            setState(() {
              _advancedList.add({
                'index': i,
                'name': '第 ${i + 1} 天',
                'date':
                    '${date.year}/${date.month < 10 ? '0${date.month}' : date.month}/${date.day < 10 ? '0${date.day}' : date.day}',
                '_date': date,
                'completion': completion,
              });
            });
          }
          setState(() {
            _advancedWidget = Container(
              child: Container(
                child: ListView(
                  children: <Widget>[
                    if (data['advanceType'] != null)
                      Container(
                        margin: EdgeInsets.all(4),
                        child: Center(
                          child: Text('進階肌膚分類: ${data['advanceType']}', style: TextStyle(fontSize: 18),),
                        ),
                      ),
                    for (var AList in _advancedList)
                      InkWell(
                        child: Card(
                          margin: EdgeInsets.all(4),
                          color: AList['completion'] > 0
                              ? Colors.lightGreen
                              : themeData.cardColor,
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Expanded(
                                child: Column(
                                  mainAxisSize: MainAxisSize.min,
                                  crossAxisAlignment:
                                      CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Container(
                                      padding: const EdgeInsets.only(
                                          left: 8.0, top: 8),
                                      child: Text(
                                        '${AList['name']}',
                                        style: AppTheme.getTextStyle(
                                            themeData.textTheme.subtitle1,
                                            fontWeight: FontWeight.w500),
                                        maxLines: 5,
                                      ),
                                    ),
                                    Container(
                                      padding: const EdgeInsets.only(
                                          left: 8.0, top: 8, bottom: 8),
                                      child: Text(
                                        '${AList['date']}',
                                        style: AppTheme.getTextStyle(
                                            themeData.textTheme.subtitle2,
                                            fontWeight: FontWeight.w400),
                                        maxLines: 5,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Padding(
                                padding:
                                    const EdgeInsets.fromLTRB(0, 0, 10, 0),
                                child: Icon(
                                  AList['completion'] == 0
                                      ? MdiIcons.circleOutline
                                      : AList['completion'] == 1
                                          ? MdiIcons.circleHalfFull
                                          : MdiIcons.circle,
                                  color: themeData.colorScheme.onBackground
                                      .withAlpha(200),
                                  size: 24.0,
                                ),
                              ),
                            ],
                          ),
                        ),
                        onTap: () {
                          if (DateTime.parse(AList['_date'].toString())
                              .isBefore(DateTime.now())) {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => Advanced(
                                        index: AList['index'],
                                        date: AList['_date'])));
                          }
                        },
                      )
                  ],
                ),
              ),
            );
          });
        }
      }
    } else {
      // 期間內
      _advancedList.clear();
      for (var i = 0; i < 28; i++) {
        DateTime date = DateTime.now().add(new Duration(days: i));
        setState(() {
          _advancedList.add({
            'index': i,
            'name': '第 ${i + 1} 天',
            'date':
                '${date.year}/${date.month < 10 ? '0${date.month}' : date.month}/${date.day < 10 ? '0${date.day}' : date.day}',
            '_date': date,
            'completion': 0,
          });
        });
      }
      setState(() {
        _advancedWidget = Container(
          child: Container(
            child: ListView.builder(
              itemCount: _advancedList.length,
              itemBuilder: (context, index) {
                return InkWell(
                  child: Card(
                    margin: EdgeInsets.all(4),
                    color: _advancedList[index]['completion'] > 0
                        ? Colors.lightGreen
                        : themeData.cardColor,
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Expanded(
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Container(
                                padding: const EdgeInsets.only(
                                    left: 8.0, top: 8),
                                child: Text(
                                  '${_advancedList[index]['name']}',
                                  style: AppTheme.getTextStyle(
                                      themeData.textTheme.subtitle1,
                                      fontWeight: FontWeight.w500),
                                  maxLines: 5,
                                ),
                              ),
                              Container(
                                padding: const EdgeInsets.only(
                                    left: 8.0, top: 8, bottom: 8),
                                child: Text(
                                  '${_advancedList[index]['date']}',
                                  style: AppTheme.getTextStyle(
                                      themeData.textTheme.subtitle2,
                                      fontWeight: FontWeight.w400),
                                  maxLines: 5,
                                ),
                              ),
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.fromLTRB(0, 0, 10, 0),
                          child: Icon(
                            _advancedList[index]['completion'] == 0
                                ? MdiIcons.circleOutline
                                : _advancedList[index]['completion'] == 1
                                    ? MdiIcons.circleHalfFull
                                    : MdiIcons.circle,
                            color: themeData.colorScheme.onBackground
                                .withAlpha(200),
                            size: 24.0,
                          ),
                        ),
                      ],
                    ),
                  ),
                  onTap: () {},
                );
              },
            ),
          ),
        );
      });
    }
  });
});
import

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...