'getNotes' 方法在 null 上被调用。接收者:空尝试调用:getNotes

时间:2020-12-19 15:09:11

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

'getNotes' 方法在 null 上被调用。接收者: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(),
        );
  }
}

1 个答案:

答案 0 :(得分:0)

您可以先尝试创建 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),
            );
            },
          );
        },
      ),
    );
  }
}