提问人:eddin99 提问时间:4/4/2023 最后编辑:eddin99 更新时间:4/4/2023 访问量:176
需要一个 identifier.dart(missing_identifier) Flutter, dart
Expected an identifier.dart(missing_identifier) Flutter, dart
问:
我一直在努力处理这段代码,试图确保它平稳运行,没有任何错误。然而,尽管我尽了最大努力,代码仍然出现故障并产生错误。这对我来说非常令人沮丧,因为我在这个项目上投入了大量的时间和精力。因此,我现在向您寻求帮助,以找出问题并找到解决方案。
我已经尝试修改代码中的括号和括号,因为我相信这可能是问题的根源。尽管我尝试过,但代码仍然拒绝合作。因此,我恳请您帮助我找到代码中的错误,以便我们最终可以使其正常工作。提前感谢您的帮助。
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
void main() => runApp(TodoApp());
class TodoApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Todo App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: TodoListScreen(),
);
}
}
class Todo {
final int id;
final String title;
bool isDone;
Todo({
required this.id,
required this.title,
this.isDone = false,
});
factory Todo.fromJson(Map<String, dynamic> json) {
return Todo(
id: json['id'],
title: json['title'],
isDone: json['isDone'],
);
}
}
class TodoListScreen extends StatefulWidget {
@override
_TodoListScreenState createState() => _TodoListScreenState();
}
class _TodoListScreenState extends State<TodoListScreen> {
final TextEditingController _todoTitleController = TextEditingController();
final List<Todo> _todos = [];
@override
void initState() {
super.initState();
_fetchTodos();
}
void _fetchTodos() async {
final response = await http.get(Uri.parse('https://todoapp-api.apps.k8s.gu.se/todos'));
if (response.statusCode == 200) {
final jsonList = jsonDecode(response.body) as List<dynamic>;
final todos = jsonList.map((json) => Todo.fromJson(json)).toList();
setState(() {
_todos.clear();
_todos.addAll(todos);
});
} else {
// Handle error
}
}
void _addTodo() async {
final newTodoTitle = _todoTitleController.text.trim();
if (newTodoTitle.isNotEmpty) {
final response = await http.post(
Uri.parse('https://todoapp-api.apps.k8s.gu.se/todos'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'title': newTodoTitle, 'isDone': false}),
);
if (response.statusCode == 201) {
final newTodo = Todo.fromJson(jsonDecode(response.body));
setState(() {
_todos.add(newTodo);
});
} else {
// Handle error
}
_todoTitleController.clear();
}
}
void _toggleTodoStatus(Todo todo) async {
final response = await http.patch(
Uri.parse('https://todoapp-api.apps.k8s.gu.se/todos/${todo.id}'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'isDone': !todo.isDone}),
);
if (response.statusCode == 200) {
setState(() {
todo.isDone = !todo.isDone;
});
} else {
// Handle error
}
}
void _deleteTodo(Todo todo) async {
final response = await http.delete(Uri.parse('https://todoapp-api.apps.k8s.gu.se/todos/${todo.id}'));
if (response.statusCode == 204) {
setState(() {
_todos.remove(todo);
});
} else {
// Handle error
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Todo List'),
),
body: ListView.builder(
itemCount: _todos.length,
itemBuilder: ()
)
);
}
}
答: 暂无答案
评论