需要一个 identifier.dart(missing_identifier) Flutter, dart

Expected an identifier.dart(missing_identifier) Flutter, dart

提问人:eddin99 提问时间:4/4/2023 最后编辑:eddin99 更新时间:4/4/2023 访问量:176

问:

我一直在努力处理这段代码,试图确保它平稳运行,没有任何错误。然而,尽管我尽了最大努力,代码仍然出现故障并产生错误。这对我来说非常令人沮丧,因为我在这个项目上投入了大量的时间和精力。因此,我现在向您寻求帮助,以找出问题并找到解决方案。

我已经尝试修改代码中的括号和括号,因为我相信这可能是问题的根源。尽管我尝试过,但代码仍然拒绝合作。因此,我恳请您帮助我找到代码中的错误,以便我们最终可以使其正常工作。提前感谢您的帮助。

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: ()
      )
    );
  }
}

Flutter dart 处理 语法错误

评论

0赞 nvoigt 4/4/2023
你得到什么错误?是编译错误,运行时错误吗?哪条线?
0赞 nvoigt 4/4/2023
这看起来不像“Angular Dart”。你确定吗?“flutter-animation”和“flutter-dependencies”与它有什么关系?
0赞 eddin99 4/4/2023
@nvoigt最后七行。
0赞 nvoigt 4/4/2023
您认为“itemBuilder:()”有什么作用?你希望它做什么?
0赞 eddin99 4/4/2023
@nvoigt我只是希望待办事项正常工作,那么我应该删除它。

答: 暂无答案