简体   繁体   English

在 null 上调用了方法“getNotes”。 接收方:null 尝试调用:getNotes

[英]The method 'getNotes' was called on null. Receiver: null Tried calling: getNotes

The method 'getNotes' was called on null.在 null 上调用了方法“getNotes”。 Receiver: null Tried calling: getNotes.接收方:null 尝试调用:getNotes。 How can I solve this problem.-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------我怎么解决这个问题。 - - - - - - - - - - - - - - - - - - - - - - -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------

home.dart主页.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 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 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(),
        );
  }
}

You can try to create an instance of your FirestoreService first, and call the getNotes method later.您可以先尝试创建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),
            );
            },
          );
        },
      ),
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 null 上调用了方法“*”。 接收方:null 尝试调用:*(null) - The method '*' was called on null. Receiver: null Tried calling: *(null) Flutter:方法 &#39;/&#39; 在 null 上被调用。 接收者:空尝试调用:/(1080.0) - Flutter: The method '/' was called on null. Receiver: null Tried calling: /(1080.0) NoSuchMethodError: 在 null 上调用了方法“[]”。 接收者:null 尝试调用:[](&quot;favorites&quot;) - NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: []("favorites") 在 null 上调用了方法“toLowerCase”。 接收方:null 尝试调用:toLowerCase() - The method 'toLowerCase' was called on null. Receiver: null Tried calling: toLowerCase() 在 null 上调用了方法“[]”。 接收方:null 尝试调用:[]("temp") - The method '[]' was called on null. Receiver: null Tried calling: []("temp") 在 null 上调用了方法“getUserData”。 接收方:null 尝试调用:getUserData() - The method 'getUserData' was called on null. Receiver: null Tried calling: getUserData() 在 null 上调用了方法“[]”。接收方:null 尝试调用:[]("id") - The method '[]' was called on null. Receiver: null Tried calling: []("id") 在 null 上调用了方法“[]”。接收方:null 尝试调用:[]("displayName") - The method '[]' was called on null. Receiver: null Tried calling: []("displayName") 方法 &#39;+&#39; 在 null 上被调用。 接收者:空尝试调用:+(123) - The method '+' was called on null. Receiver: null Tried calling: +(123) 方法 &#39;[]&#39; 在 null 上被调用。 接收者:空尝试调用:[](0) - The method '[]' was called on null. Receiver: null Tried calling: [](0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM