【问题标题】:The method 'getNotes' was called on null. Receiver: null Tried calling: getNotes在 null 上调用了方法“getNotes”。接收方:null 尝试调用:getNotes
【发布时间】:2021-03-29 22:41:16
【问题描述】:

在 null 上调用了方法“getNotes”。接收者:null 尝试调用:getNotes。我怎么解决这个问题。 - - - - - - - - - - - - - - - - - - - - - - -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------

home.dart

import 'package:flutter/material.dart';
import 'package:notesapp/data/firestore_services.dart';
import 'package:notesapp/data/model/note.dart';

class HomePage extends StatelessWidget{
  @override
  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: Text('Notes'),
      ),
      body: StreamBuilder(
        stream: FirestoreService().getNotes() ,
        builder: (BuildContext context, AsyncSnapshot<List<Note>> snapshot){
          if(snapshot.hasError || snapshot.hasData)
            CircularProgressIndicator();
          return ListView.builder(
            itemCount: snapshot.data.length,
            itemBuilder: (BuildContext context, int index){
              Note note = snapshot.data[index];
            return ListTile(
              title: Text(note.title),
              subtitle: Text(note.description),
            );
            },
          );
        },
      ),
    );
  }
}

note.dart

class Note{
  final String title;
  final String description;
  final String id;

  Note({this.title, this.description, this.id});

  Note.fromMap(Map<String,dynamic>data, String id):
    title=data["title"],
    description=data['description'],
    id=id;

}

firestore_services.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';

import 'model/note.dart';

class FirestoreService {
  static final FirestoreService _firestoreService =
      FirestoreService._internal();
  Firestore _db = Firestore.instance;

  FirestoreService._internal();

  factory FirestoreService() {
    return _firestoreService;
  }

  Stream<List<Note>> getNotes() {
    return _db.collection('notes').snapshots().map(
          (snapshot) => snapshot.documents.map(
            (doc) => Note.fromMap(doc.data(), doc.documentID),
          ).toList(),
        );
  }
}

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies dart-pub


    【解决方案1】:

    您可以先尝试创建FirestoreService 的实例,然后再调用getNotes 方法。

    class HomePage extends StatelessWidget{
    
     final firestoreService = FirestoreService(); // Here we get an instance!
    
     @override
      Widget build(BuildContext context){
        return Scaffold(
          appBar: AppBar(
            title: Text('Notes'),
          ),
          body: StreamBuilder(
            stream: firestoreService.getNotes(), // here 
            builder: (BuildContext context, AsyncSnapshot<List<Note>> snapshot){
              if(snapshot.hasError || snapshot.hasData)
                CircularProgressIndicator();
              return ListView.builder(
                itemCount: snapshot.data.length,
                itemBuilder: (BuildContext context, int index){
                  Note note = snapshot.data[index];
                return ListTile(
                  title: Text(note.title),
                  subtitle: Text(note.description),
                );
                },
              );
            },
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-26
      • 2020-12-06
      • 2021-01-14
      • 2020-05-24
      • 2022-08-05
      • 2021-04-17
      • 2020-07-03
      • 1970-01-01
      • 2022-09-26
      相关资源
      最近更新 更多