如何解决使用 todo 模块抖动/放大错误
我正在学习放大和颤振的教程,但遇到了一些控制台错误。我正在关注此https://docs.amplify.aws/start/getting-started/integrate/q/integration/flutter#creating-a-todo
虽然我似乎无法清除错误。我将 amplify 核心更改为 amplify_Flutter,这似乎清除了一些错误,但现在它说我的 todo 模块有错误。
任何帮助都会有所帮助。
这是控制台中的错误
lib/main.dart:41:8: Error: Type 'Todo' not found.
List<Todo> _todos = [];
^^^^
lib/main.dart:146:14: Error: Type 'Todo' not found.
final List<Todo> todos;
^^^^
lib/main.dart:162:9: Error: Type 'Todo' not found.
final Todo todo;
^^^^
lib/main.dart:41:8: Error: 'Todo' isn't a type.
List<Todo> _todos = [];
^^^^
lib/main.dart:70:47: Error: The getter 'Todo' isn't defined for the class '_TodosPageState'.
- '_TodosPageState' is from 'package:amplified_todo/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter,or defining a getter or field named 'Todo'.
_subscription = Amplify.DataStore.observe(Todo.classtype).listen((event) {
^^^^
lib/main.dart:107:12: Error: 'Todo' isn't a type.
List<Todo> updatedTodos = await Amplify.DataStore.query(Todo.classtype);
^^^^
lib/main.dart:107:63: Error: The getter 'Todo' isn't defined for the class '_TodosPageState'.
- '_TodosPageState' is from 'package:amplified_todo/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter,or defining a getter or field named 'Todo'.
List<Todo> updatedTodos = await Amplify.DataStore.query(Todo.classtype);
^^^^
lib/main.dart:125:8: Error: Duplicated named argument 'body'.
body: _isLoading
^^^^
lib/main.dart:146:14: Error: 'Todo' isn't a type.
final List<Todo> todos;
^^^^
lib/main.dart:155:61: Error: The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<Widget>'.
- 'List' is from 'dart:core'.
- 'Widget' is from 'package:Flutter/src/widgets/framework.dart' ('../../src_code/Flutter/packages/Flutter/lib/src/widgets/framework.dart').
children: todos.map((todo) => TodoItem(todo: todo)).toList())
^
lib/main.dart:162:9: Error: 'Todo' isn't a type.
final Todo todo;
^^^^
lib/main.dart:178:5: Error: 'Todo' isn't a type.
Todo updatedTodo = todo.copyWith(isComplete: !todo.isComplete);
^^^^
lib/main.dart:241:5: Error: 'Todo' isn't a type.
Todo newTodo = Todo(
^^^^
lib/main.dart:241:20: Error: The method 'Todo' isn't defined for the class '_AddTodoFormState'.
- '_AddTodoFormState' is from 'package:amplified_todo/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method,or defining a method named 'Todo'.
Todo newTodo = Todo(
^^^^
main.dart 文件
// dart async library we will refer to when setting up real time updates
import 'dart:async';
// Flutter and ui libraries
import 'package:Flutter/material.dart';
// amplify packages we will need to use
import 'package:amplify_Flutter/amplify.dart';
import 'package:amplify_datastore/amplify_datastore.dart';
// amplify configuration and models that should have been generated for you
import 'amplifyconfiguration.dart';
import 'models/ModelProvider.dart';
import 'models/UpdatedTodo.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Amplified Todo',home: TodosPage(),);
}
}
class TodosPage extends StatefulWidget {
@override
_TodosPageState createState() => _TodosPageState();
}
class _TodosPageState extends State<TodosPage> {
// subscription to Todo model update events - to be initialized at runtime
late StreamSubscription _subscription;
// loading ui state - initially set to a loading state
bool _isLoading = true;
// list of Todos - initially empty
List<Todo> _todos = [];
// amplify plugins
final AmplifyDataStore _dataStorePlugin =
AmplifyDataStore(modelProvider: ModelProvider.instance);
@override
void initState() {
// kick off app initialization
_initializeApp();
super.initState();
}
@override
void dispose() {
// cancel the subscription when the state is removed from the tree
_subscription.cancel();
super.dispose();
}
Future<void> _initializeApp() async {
// configure Amplify
await _configureAmplify();
// listen for updates to Todo entries by passing the Todo classtype to
// Amplify.DataStore.observe() and when an update event occurs,fetch the
// todo list
//
// note this strategy may not scale well with larger number of entries
_subscription = Amplify.DataStore.observe(Todo.classtype).listen((event) {
_fetchTodos();
});
// fetch Todo entries from DataStore
await _fetchTodos();
// after both configuring Amplify and fetching Todo entries,update loading
// ui state to loaded state
setState(() {
_isLoading = false;
});
}
Future<void> _configureAmplify() async {
try {
// add Amplify plugins
await Amplify.addplugins([_dataStorePlugin]);
// configure Amplify
//
// note that Amplify cannot be configured more than once!
await Amplify.configure(amplifyconfig);
} catch (e) {
// error handling can be improved for sure!
// but this will be sufficient for the purposes of this tutorial
print('An error occurred while configuring Amplify: $e');
}
}
Future<void> _fetchTodos() async {
try {
// query for all Todo entries by passing the Todo classtype to
// Amplify.DataStore.query()
List<Todo> updatedTodos = await Amplify.DataStore.query(Todo.classtype);
// update the ui state to reflect fetched todos
setState(() {
_todos = updatedTodos;
});
} catch (e) {
print('An error occurred while querying Todos: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Todo List'),),body: Center(child: CircularProgressIndicator()),body: _isLoading
? Center(child: CircularProgressIndicator())
: TodosList(todos: _todos),floatingActionButton: FloatingActionButton.extended(
onpressed: () {
Navigator.push(
context,MaterialPageRoute(builder: (context) => AddTodoForm()),);
},tooltip: 'Add Todo',label: Row(
children: [Icon(Icons.add),Text('Add todo')],floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,);
}
}
class TodosList extends StatelessWidget {
final List<Todo> todos;
TodosList({required this.todos});
@override
Widget build(BuildContext context) {
return todos.length >= 1
? ListView(
padding: EdgeInsets.all(8),children: todos.map((todo) => TodoItem(todo: todo)).toList())
: Center(child: Text('Tap button below to add a todo!'));
}
}
class TodoItem extends StatelessWidget {
final double iconSize = 24.0;
final Todo todo;
TodoItem({required this.todo});
void _deletetodo(BuildContext context) async {
try {
// to delete data from DataStore,we pass the model instance to
// Amplify.DataStore.delete()
await Amplify.DataStore.delete(todo);
} catch (e) {
print('An error occurred while deleting Todo: $e');
}
}
Future<void> _toggleIsComplete() async {
// copy the Todo we wish to update,but with updated properties
Todo updatedTodo = todo.copyWith(isComplete: !todo.isComplete);
try {
// to update data in DataStore,we again pass an instance of a model to
// Amplify.DataStore.save()
await Amplify.DataStore.save(updatedTodo);
} catch (e) {
print('An error occurred while saving Todo: $e');
}
}
@override
Widget build(BuildContext context) {
return Card(
child: InkWell(
onTap: () {
_toggleIsComplete();
},onLongPress: () {
_deletetodo(context);
},child: Padding(
padding: const EdgeInsets.all(8.0),child: Row(children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,children: [
Text(todo.name,style:
TextStyle(fontSize: 20,fontWeight: FontWeight.bold)),Text(todo.description ?? 'No description'),],Icon(
todo.isComplete
? Icons.check_Box
: Icons.check_Box_outline_blank,size: iconSize),]),);
}
}
class AddTodoForm extends StatefulWidget {
@override
_AddTodoFormState createState() => _AddTodoFormState();
}
class _AddTodoFormState extends State<AddTodoForm> {
final _nameController = TextEditingController();
final _descriptionController = TextEditingController();
Future<void> _savetodo() async {
// get the current text field contents
String name = _nameController.text;
String description = _descriptionController.text;
// create a new Todo from the form values
// `isComplete` is also required,but should start false in a new Todo
Todo newTodo = Todo(
name: name,description: description.isNotEmpty ? description : null,isComplete: false);
try {
// to write data to DataStore,we simply pass an instance of a model to
// Amplify.DataStore.save()
await Amplify.DataStore.save(newTodo);
// after creating a new Todo,close the form
Navigator.of(context).pop();
} catch (e) {
print('An error occurred while saving Todo: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Add Todo'),body: Container(
padding: EdgeInsets.all(8.0),child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,children: [
TextFormField(
controller: _nameController,decoration: Inputdecoration(filled: true,labelText: 'Name')),TextFormField(
controller: _descriptionController,decoration:
Inputdecoration(filled: true,labelText: 'Description')),ElevatedButton(onpressed: _savetodo,child: Text('Save'))
],);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。