Flutter 使用提供商将地图状态保存到颤振应用程序

Flutter 使用提供商将地图状态保存到颤振应用程序,flutter,dart,Flutter,Dart,我有一个基于以下结构呈现的ListView。该映射有大约5个“字符串”键值,每个键值有一个10-15个ItemModels的列表。通过使用toggleDone()调用各个ItemModels,可以翻转isDone。这将在应用程序中保存bool状态,但一旦应用程序关闭,它将重置。我调查了共享的引用,但是- 我不知道如何编码这个特定的地图,然后加载必要的值 这是我在模特课上应该做的事吗, 还是单个ItemModel类 有没有办法只对布尔值进行编码,然后加载这些值 保存映射的ChangeNotifie

我有一个基于以下结构呈现的ListView。该映射有大约5个“字符串”键值,每个键值有一个10-15个ItemModels的列表。通过使用toggleDone()调用各个ItemModels,可以翻转isDone。这将在应用程序中保存bool状态,但一旦应用程序关闭,它将重置。我调查了共享的引用,但是-

  • 我不知道如何编码这个特定的地图,然后加载必要的值
  • 这是我在模特课上应该做的事吗, 还是单个ItemModel类
  • 有没有办法只对布尔值进行编码,然后加载这些值
  • 保存映射的ChangeNotifier类:

     ```
    class Data extends ChangeNotifier {
       List<ItemModel> getData(name) => items[name];
    
            Map<String, List<ItemModel>> items = {
                    'String': [
                      ItemModel(
                        title: 'Some text',
                        text:
                            'Some other text',
                        isDone: false,
                        duration: Duration(
                          hours: 0,
                          minutes: 0,
                          seconds: 10,
                        ),
                      ),
                    ]
                };
              }
    
    对于列表中的每个ItemModel,将呈现一个如下所示的项。它使用isDone的ItemModel值来设置isChecked属性

    class Item extends StatelessWidget {
      final String title;
      final String text;
      final bool isChecked;
      final Function checkboxCallback;
    
      Item(
          {this.text,
          this.title,
          this.isChecked,
          this.checkboxCallback});
    
          @override
          Widget build(BuildContext context) {
            return Padding(
              child: ListTile(
                leading: Checkbox(
                  value: isChecked,
                  onChanged: checkboxCallback,
                ),
                title: Text(
                  title,
                ),
                subtitle: isChecked
                    ? Text('')
                    : Text(
                        text,
                      ),
              ),
            );
          }
        }
    

    我感谢您提供的任何帮助,因为您提到SharedReferences只支持持久化原语值。假设您可以保证列表中
    ItemModel
    的每个实例都有唯一的密钥,那么您可以做的就是围绕SharedReferences编写自己的包装器:

    import 'package:shared_preferences/shared_preferences.dart';
    
    abstract class SharedPreferencesKeys {
      static const title = 'title';
      static const text = 'text';
      static const isDone = 'isDone';
      static const duration = 'duration';
    }
    
    class SharedPreferencesImpl {
      SharedPreferences _preferences;
      Future<SharedPreferences> get preferences async => _preferences ??= await SharedPreferences.getInstance();
    
      Future<void> write(String key, ItemModel value) async {
        final sharedPrefs = await preferences;
        await sharedPrefs.setString('$key_${SharedPreferencesKeys.title}', value.title);
        await sharedPrefs.setString('$key_${SharedPreferencesKeys.text}', value.text);
        await sharedPrefs.setBool('$key_${SharedPreferencesKeys.isDone}', value.isDone);
        await sharedPrefs.setInt('$key_${SharedPreferencesKeys.duration}', value.duration.microseconds);
      }
    
      Future<ItemModel> read(String key) async {
        final sharedPrefs = await preferences;
        final title = await sharedPrefs.getString('$key_${SharedPreferencesKeys.title}');
        final text = await sharedPrefs.getString('$key_${SharedPreferencesKeys.text}');
        final isDone = await sharedPrefs.getBool('$key_${SharedPreferencesKeys.isDone}');
        final microseconds = await sharedPrefs.getInt('$key_${SharedPreferencesKeys.duration}');
        return ItemModel(
          title: title,
          text: text,
          isDone: isDone,
          duration: Duration(microseconds: microseconds),
        );
      }
    
      Future<void> delete(String key) async {
        final sharedPrefs = await preferences;
        await sharedPrefs.remove('$key_${SharedPreferencesKeys.title}');
        await sharedPrefs.remove('$key_${SharedPreferencesKeys.text}');
        await sharedPrefs.remove('$key_${SharedPreferencesKeys.isDone}');
        await sharedPrefs.remove('$key_${SharedPreferencesKeys.duration}');
      }
    }
    
    import'package:shared_preferences/shared_preferences.dart';
    抽象类SharedReferenceKeys{
    静态常量标题='title';
    静态常量text='text';
    静态常量isDone='isDone';
    静态常量持续时间=‘持续时间’;
    }
    类SharedReferencesImpl{
    共享引用\u首选项;
    Future get preferences async=>_preferences???=等待SharedReferences.getInstance();
    未来写入(字符串键、ItemModel值)异步{
    最终sharedPrefs=等待首选项;
    等待SharedReferencesSetString('$key_${SharedReferencesKeys.title}',value.title);
    等待sharedPrefs.setString('$key_${SharedPreferenceKeys.text}',value.text);
    等待sharedPrefs.setBool('$key_${sharedpreferenceskies.isDone}',value.isDone);
    等待sharedPrefs.setInt('$key_${sharedpreferenceskies.duration}',value.duration.microseconds);
    }
    未来读取(字符串键)异步{
    最终sharedPrefs=等待首选项;
    final title=wait sharedPrefs.getString(“$key_${sharedpreferenceskies.title}”);
    final text=wait sharedPrefs.getString(“$key_${sharedpreferenceskies.text}”);
    final isDone=wait sharedPrefs.getBool('$key_${sharedpreferenceskies.isDone}');
    final microseconds=wait sharedPrefs.getInt(“$key_${sharedpreferenceskies.duration}”);
    返回项模型(
    标题:标题,,
    文本:文本,
    isDone:isDone,
    持续时间:持续时间(微秒:微秒),
    );
    }
    未来删除(字符串键)异步{
    最终sharedPrefs=等待首选项;
    等待SharedReferences.remove('$key_${SharedReferencesKeys.title}');
    等待SharedReferences.remove('$key_${SharedReferenceKeys.text}');
    等待SharedReferences.remove('$key_${SharedReferencesKeys.isDone}');
    等待SharedReferences.remove('$key_${SharedReferenceKeys.duration}');
    }
    }
    
    如您所述,SharedReference仅支持持久化原语值。假设您可以保证列表中
    ItemModel
    的每个实例都有唯一的密钥,那么您可以做的就是围绕SharedReferences编写自己的包装器:

    import 'package:shared_preferences/shared_preferences.dart';
    
    abstract class SharedPreferencesKeys {
      static const title = 'title';
      static const text = 'text';
      static const isDone = 'isDone';
      static const duration = 'duration';
    }
    
    class SharedPreferencesImpl {
      SharedPreferences _preferences;
      Future<SharedPreferences> get preferences async => _preferences ??= await SharedPreferences.getInstance();
    
      Future<void> write(String key, ItemModel value) async {
        final sharedPrefs = await preferences;
        await sharedPrefs.setString('$key_${SharedPreferencesKeys.title}', value.title);
        await sharedPrefs.setString('$key_${SharedPreferencesKeys.text}', value.text);
        await sharedPrefs.setBool('$key_${SharedPreferencesKeys.isDone}', value.isDone);
        await sharedPrefs.setInt('$key_${SharedPreferencesKeys.duration}', value.duration.microseconds);
      }
    
      Future<ItemModel> read(String key) async {
        final sharedPrefs = await preferences;
        final title = await sharedPrefs.getString('$key_${SharedPreferencesKeys.title}');
        final text = await sharedPrefs.getString('$key_${SharedPreferencesKeys.text}');
        final isDone = await sharedPrefs.getBool('$key_${SharedPreferencesKeys.isDone}');
        final microseconds = await sharedPrefs.getInt('$key_${SharedPreferencesKeys.duration}');
        return ItemModel(
          title: title,
          text: text,
          isDone: isDone,
          duration: Duration(microseconds: microseconds),
        );
      }
    
      Future<void> delete(String key) async {
        final sharedPrefs = await preferences;
        await sharedPrefs.remove('$key_${SharedPreferencesKeys.title}');
        await sharedPrefs.remove('$key_${SharedPreferencesKeys.text}');
        await sharedPrefs.remove('$key_${SharedPreferencesKeys.isDone}');
        await sharedPrefs.remove('$key_${SharedPreferencesKeys.duration}');
      }
    }
    
    import'package:shared_preferences/shared_preferences.dart';
    抽象类SharedReferenceKeys{
    静态常量标题='title';
    静态常量text='text';
    静态常量isDone='isDone';
    静态常量持续时间=‘持续时间’;
    }
    类SharedReferencesImpl{
    共享引用\u首选项;
    Future get preferences async=>_preferences???=等待SharedReferences.getInstance();
    未来写入(字符串键、ItemModel值)异步{
    最终sharedPrefs=等待首选项;
    等待SharedReferencesSetString('$key_${SharedReferencesKeys.title}',value.title);
    等待sharedPrefs.setString('$key_${SharedPreferenceKeys.text}',value.text);
    等待sharedPrefs.setBool('$key_${sharedpreferenceskies.isDone}',value.isDone);
    等待sharedPrefs.setInt('$key_${sharedpreferenceskies.duration}',value.duration.microseconds);
    }
    未来读取(字符串键)异步{
    最终sharedPrefs=等待首选项;
    final title=wait sharedPrefs.getString(“$key_${sharedpreferenceskies.title}”);
    final text=wait sharedPrefs.getString(“$key_${sharedpreferenceskies.text}”);
    final isDone=wait sharedPrefs.getBool('$key_${sharedpreferenceskies.isDone}');
    final microseconds=wait sharedPrefs.getInt(“$key_${sharedpreferenceskies.duration}”);
    返回项模型(
    标题:标题,,
    文本:文本,
    isDone:isDone,
    持续时间:持续时间(微秒:微秒),
    );
    }
    未来删除(字符串键)异步{
    最终sharedPrefs=等待首选项;
    等待SharedReferences.remove('$key_${SharedReferencesKeys.title}');
    等待SharedReferences.remove('$key_${SharedReferenceKeys.text}');
    等待SharedReferences.remove('$key_${SharedReferencesKeys.isDone}');
    等待SharedReferences.remove('$key_${SharedReferenceKeys.duration}');
    }
    }
    
    谢谢,我会试试这个!你知道为什么我会得到错误“'wait'应用于'int',这不是一个'未来'”?您对实例化SharedReferenceSimpl类的最佳位置有何看法?我需要查看您的代码,显然您调用了一个返回
    int
    而不是
    Future
    的错误函数,但我无法告诉您是哪一个函数而不看一下。至于实例化的位置,这取决于您的体系结构。如果您刚刚开始使用Flutter,请查看BLoC模式。另外,请看一下
    provider
    riverpod
    依赖注入包。谢谢,我会试试这个!你知道为什么我会得到错误“'wait'应用于'int',这不是一个'未来'”?您对实例化SharedReferenceSimpl类的最佳位置有何看法?我需要查看您的代码,显然您调用了一个返回
    int
    而不是
    Fut的错误函数