制作一個串列,將專案添加到資料庫中。從main.dart切換到有串列的頁面后,打不開,寫報錯。在此處輸入圖片說明
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
late String _userToDo;
List todoList = [];
void initFirebase() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(Home());
}
@override
void initState() {
super.initState();
initFirebase();
todoList.addAll(['Milk', 'Car', 'Sugar']);
}
void _menuOpen() {
Navigator.of(context).push(
MaterialPageRoute(builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Menu'),),
body: Row(
children: [
Padding(padding: EdgeInsets.only(left: 15)),
ElevatedButton(onPressed: () {
Navigator.pop(context);
Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false);
},
child: Text('Home')),
Padding(padding: EdgeInsets.only(left: 15)),
Text('Home old')
],
)
);
})
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[900],
appBar: AppBar(
title: Text('Список дел'),
actions: [
IconButton(onPressed: _menuOpen,
icon: Icon(Icons.menu_outlined),
)
],
),
body: ListView.builder(
itemCount: todoList.length,
itemBuilder: (BuildContext context, int index){
return Dismissible(
key: Key(todoList[index]),
child: Card(
child: ListTile(
title: Text(todoList[index]),
trailing: IconButton(
icon: Icon(Icons.delete_sweep,
color: Colors.redAccent,
), onPressed: () {
setState(() {
todoList.removeAt(index);
});
},
)
),
),
onDismissed: (direction) {
// if(direction == DismissDirection.startToEnd)
setState(() {
todoList.removeAt(index);
});
},
);
}
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.green,
onPressed: () {
showDialog(context: context, builder: (BuildContext context){
return AlertDialog(
title: Text('Добавить'),
content: TextField(
onChanged: (String value){
_userToDo = value;
},
),
actions: [
ElevatedButton(onPressed: (){
FirebaseFirestore.instance.collection('items').add({'item': _userToDo});
Navigator.of(context).pop();
}, child: Text('Добавить')
)
],
);
});
},
child: Icon(Icons.add_comment_outlined,
color: Colors.white,
),
),
);
}
}
每個人都知道錯誤。
在構建 Home(state: _HomeState#17f50): No MediaQuery widget 祖先時拋出了以下斷言。
腳手架小部件需要 MediaQuery 小部件祖先。找不到 MediaQuery 祖先的特定小部件是: Scaffold 臟狀態:ScaffoldState#4d9ee(生命周期狀態:初始化,股票代碼:跟蹤 2 個股票代碼)受影響小部件的所有權鏈是:“Scaffold ← Home ← [root]”
從傳遞給 MediaQuery.of() 的背景關系開始,找不到任何 MediaQuery 祖先。這可能是因為您沒有添加 WidgetsApp、CupertinoApp 或 MaterialApp 小部件(這些小部件引入了 MediaQuery),或者如果您使用的背景關系來自這些小部件上方的小部件,則可能會發生這種情況。
uj5u.com熱心網友回復:
部分錯誤說:This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget。
所以在你的 Build 方法中,你可以用 a 包裹你的腳手架,MaterialApp()它應該可以作業。
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(...),
);
}
uj5u.com熱心網友回復:
根據您的建議進行設定。導航和彈出視窗停止作業。
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
late String _userToDo;
List todoList = [];
void initFirebase() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(Home());
}
@override
void initState() {
super.initState();
initFirebase();
todoList.addAll(['Milk', 'Car', 'Sugar']);
}
void _menuOpen() {
Navigator.of(context).push(
MaterialPageRoute(builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Menu'),),
body: Row(
children: [
Padding(padding: EdgeInsets.only(left: 15)),
ElevatedButton(onPressed: () {
Navigator.pop(context);
Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false);
},
child: Text('Home')),
Padding(padding: EdgeInsets.only(left: 15)),
Text('Home old')
],
)
);
})
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.grey[900],
appBar: AppBar(
title: Text('Список дел'),
actions: [
IconButton(onPressed: _menuOpen,
icon: Icon(Icons.menu_outlined),
)
],
),
body: ListView.builder(
itemCount: todoList.length,
itemBuilder: (BuildContext context, int index){
return Dismissible(
key: Key(todoList[index]),
child: Card(
child: ListTile(
title: Text(todoList[index]),
trailing: IconButton(
icon: Icon(Icons.delete_sweep,
color: Colors.redAccent,
), onPressed: () {
setState(() {
todoList.removeAt(index);
});
},
)
),
),
onDismissed: (direction) {
// if(direction == DismissDirection.startToEnd)
setState(() {
todoList.removeAt(index);
});
},
);
}
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.green,
onPressed: () {
showDialog(context: context, builder: (BuildContext context){
return AlertDialog(
title: Text('Добавить'),
content: TextField(
onChanged: (String value){
_userToDo = value;
},
),
actions: [
ElevatedButton(onPressed: (){
FirebaseFirestore.instance.collection('items').add({'item': _userToDo});
Navigator.of(context).pop();
}, child: Text('Добавить')
)
],
);
});
},
child: Icon(Icons.add_comment_outlined,
color: Colors.white,
),
),
),
);
}
}
處理手勢時拋出以下斷言:未找到 MaterialLocalizations。
主頁小部件需要由本地化小部件祖先提供 MaterialLocalizations。材料庫使用本地化來生成訊息、標簽和縮寫。
要引入 MaterialLocalizations,請在應用程式的根目錄使用 MaterialApp 以自動包含它們,或者添加帶有 MaterialLocalizations 委托的 Localization 小部件。
找不到 MaterialLocalizations 祖先的特定小部件是: Home state: _HomeState#8899d 這個小部件的祖先是: : [root] renderObject: RenderView#1dbbb
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/375653.html
