简体   繁体   English

使用提供程序 Package Flutter 时如何修复错误

[英]How to Fix Error when using Provider Package Flutter

I'm creating an app like a note that contains a list of activities that you can add.我正在创建一个应用程序,例如一个包含您可以添加的活动列表的笔记。 I use the provider package to save the changes to the list.我使用提供者 package 将更改保存到列表中。 The list is created by ListView.builder , and the child is a ListTile consisting of text and a checkbox.该列表由ListView.builder创建,子项是由文本和复选框组成的ListTile there is no error in my coding, but in debugging there is an error "if you want to expose a variable that can be anything, consider to changing dynamic to object instead" like the image I gave.我的编码没有错误,但在调试中出现错误“如果你想公开一个可以是任何东西的变量,请考虑将动态更改为 object”,就像我给出的图像一样。 how can i fix it?我该如何解决?

Main.dart主.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todoey_flutter/models/task_data.dart';
import 'screens/tasks_screen.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => TaskData(),
      lazy: false,
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        home: TasksScreen(),
      ),
    );
  }
}

task_list.dart task_list.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'task_tile.dart';

class TaskList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer(
      builder: (context, taskDataObject, child) {
        return ListView.builder(
          itemBuilder: (context, index) {
            return TaskTile(
              theTask: taskDataObject.tasks[index].name,
              isChecked: taskDataObject.tasks[index].isDone,
              checkboxCallback: (checkboxState) {
                taskDataObject.updateTask(taskDataObject.tasks[index]);
              },
            );
          },
          itemCount: taskDataObject.taskCount,
        );
      },
    );
  }
}

task_tile.dart task_tile.dart

import 'package:flutter/material.dart';

class TaskTile extends StatelessWidget {
  TaskTile({
    this.isChecked,
    this.theTask,
    this.checkboxCallback,
    this.onLongPressCallback,
  });

  final bool isChecked;
  final String theTask;
  final Function checkboxCallback;
  final Function onLongPressCallback;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      onLongPress: onLongPressCallback,
      title: Text(
        theTask,
        style: TextStyle(
            decoration: isChecked ? TextDecoration.lineThrough : null),
      ),
      trailing: Checkbox(
        activeColor: Colors.lightBlueAccent,
        value: isChecked,
        onChanged: checkboxCallback,
      ),
    );
  }
}

the Provider class提供者 class

import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:todoey_flutter/models/task.dart';

class TaskData extends ChangeNotifier {
  List<Task> _tasks = [
    Task(name: 'cook'),
    Task(name: 'reading'),
    Task(name: 'sleding')
  ];

  int get taskCount {
    return _tasks.length;
  }

  UnmodifiableListView<Task> get tasks {
    return UnmodifiableListView(_tasks);
  }

  void addTask (String newTaskTitle) {
    final task = Task(name: newTaskTitle);
    _tasks.add(task);
    notifyListeners();
  }

  void updateTask (Task task){
    task.toggleDone();
    notifyListeners();
  }

  void deleteTask (Task task){
    _tasks.remove(task);
    notifyListeners();
  }
}

The Error Message错误信息

You have to use Consumer of T , instead of only Consumer .您必须使用ConsumerT ,而不仅仅是Consumer So, change task_list.dart to be like this:因此,将task_list.dart更改为如下所示:

class TaskList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<TaskData>( // <--- Here, insert <TaskData> after Consumer
      builder: (context, taskDataObject, child) {
        // ...
      },
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在flutter上使用最新版本的camera包时如何修复错误 - How to fix error when using the latest version of the camera package on the flutter 使用 flutter_svg package 时如何修复此 nullOk 错误? - How to fix this nullOk error when using the flutter_svg package? When I am using the provider package in Flutter to load data from an API into a list it repeatedly calls the API, how do I fix it? - When I am using the provider package in Flutter to load data from an API into a list it repeatedly calls the API, how do I fix it? 在 flutter 应用程序中添加提供程序 package 时出错 - Error when add Provider package in flutter app 在 flutter 中使用 path_provider package 时出错 - Error using path_provider package in flutter 如何修复 Flutter 中 Provider package 的“Failed assertion: 'create:= null'? is not true” 错误? - How to fix "Failed assertion: 'create != null': is not true" error with Provider package in Flutter? 如何使用 Flutter 中的 Provider 包更改图像 - How to change images using the Provider Package in Flutter 与 Flutter 提供程序包相关的错误 - Error related to Flutter provider package 将提供程序包添加到flutter Web项目时出错 - Error when adding provider package to flutter web project 使用 Flutter 中的 package 与提供程序的连接 - Using connectivity package in Flutter with provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM