I have been trying to pass data from one page to another and running into an error I’m struggling to fix:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building edit(dirty, state: _editState#8e4e4):
'[]'
Dynamic call of null.
Receiver: Instance of 'document'
Arguments: ["content"]
The relevant error-causing widget was:
edit
On tap on the list elements in home.dart it should be passed as a document object to the editor page and opened as a document:
Home.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:email_templating_app/pages/editor.dart';
import 'package:flutter/material.dart';
class document {
final String name;
final String content;
final String id;
document(this.name, this.content, this.id);
}
class home extends StatefulWidget {
@override
_homeState createState() => _homeState();
}
class _homeState extends State<home> {
var db = FirebaseFirestore.instance;
bool loading = true;
late var templates = List<document>.empty(growable: true);
//get all template documents from firestore and add them to the templates list as document objects
Future<List<document>> getTemplates() async {
List<document> templates = [];
await db.collection("templates").get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
templates.add(document(
result.data()["name"], result.data()["content"], result.id));
});
});
return templates;
}
@override
Widget build(BuildContext context) {
// return Container();
if (loading) {
getTemplates().then((value) {
setState(() {
templates = value;
});
});
setState(() {
loading = false;
});
}
return Scaffold(
appBar: AppBar(
title: Text("Email Templates"),
),
body: Center(
child: ListView.builder(
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(templates[index].name),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => edit(templates[index]),
));
},
),
);
},
itemCount: templates.length),
));
}
}
editor.dart
import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:email_templating_app/pages/Home.dart';
import 'package:flutter/material.dart';
import 'package:super_editor/super_editor.dart';
import 'package:super_editor_unofficial_helper/super_editor_unofficial_helper.dart';
class edit extends StatefulWidget {
final document doc;
edit(this.doc);
@override
_editState createState() => _editState();
}
class _editState extends State<edit> {
late MutableDocument myDoc;
late DocumentEditor docEditor;
FirebaseFirestore db = FirebaseFirestore.instance;
MutableDocument createDocument(doc) {
if (doc == null) {
myDoc = MutableDocument(
nodes: [
ParagraphNode(
id: DocumentEditor.createNodeId(),
text: AttributedText(text: 'This is a header'),
metadata: {
'blockType': header1Attribution,
},
),
ParagraphNode(
id: DocumentEditor.createNodeId(),
text: AttributedText(text: 'This is the first paragraph'),
),
],
);
return myDoc;
} else {
myDoc = mapToDocument(json.decode(doc["content"]));
return myDoc;
}
}
@override
Widget build(BuildContext context) {
docEditor = DocumentEditor(document: createDocument(widget.doc));
return Scaffold(
appBar: AppBar(
title: Text("Email Templates"),
),
body: Center(
child: SuperEditor(
editor: docEditor,
)),
floatingActionButton: FloatingActionButton(
onPressed: () async {
await db
.collection("templates")
.doc(DateTime.now().toString())
.set({
"name": "test",
"content": myDoc.toJson(),
});
print(myDoc.toJson());
},
child: Icon(Icons.save),
));
}
}
Once I can pass the data I can then implement updating the documents rather than hard coding making templates which are currently implemented this way for a demo of the project very soon.
>Solution :
Replace doc["content"] with doc.content because doc is not a map it is a class in your case.