简体   繁体   English

如何在Flutter(Dart)中更新本地JSON文件中的数据

[英]How to update data in local JSON file in Flutter (Dart)

I am using a local JSON file in my flutter app.我在我的 flutter 应用程序中使用本地 JSON 文件。 On a button click I want to change one item in same JSON file.单击按钮,我想更改同一个 JSON 文件中的一项。 The JSON file (UserData.json) is: JSON 文件 (UserData.json) 是:

        {
            "name": "name1",
            "companyName": "companyName1",
            "designation": "designation1",
            "isAdmin": "FALSE"
        },
        {
            "name": "name2",
            "companyName": "companyName2",
            "designation": "designation2",
            "isAdmin": "FALSE"
        },
        {
            "name": "name3",
            "companyName": "companyName3",
            "designation": "designation3",
            "isAdmin": "FALSE"
        },
        {
            "name": "name4",
            "companyName": "companyName4",
            "designation": "designation4",
            "isAdmin": "FALSE"
        }
  ]

The model (user_model.dart) is: model (user_model.dart) 是:

  final String name;
  final String companyName;
  final String designation;
  final String isAdmin;

  User({
    this.name,
    this.companyName,
    this.designation,
    this.isAdmin,
  });

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      name: json['name'] as String,
      companyName: json['companyName'] as String,
      designation: json['designation'] as String,
      isAdmin: json['isAdmin'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'name': name,
      'companyName': companyName,
      'designation': designation,
      'isAdmin': isAdmin,
    };
  }
}

For testing purpose, I want to update the 'companyName' of the first user in JSON file.出于测试目的,我想更新 JSON 文件中第一个用户的“companyName”。 The method(not complete yet) called on button press to is:按下按钮时调用的方法(尚未完成)是:

updateJson() async {
    var rawJSON = await json.decode(await DefaultAssetBundle.of(context)
        .loadString('user_data/UserData.json'));

    rawJSON[0]['companyName'] = "NewCompany";
    var var1 = json.encode(rawJSON);
  }

Please help me with the code to update the JSON file请帮助我更新 JSON 文件的代码

I don't think you'll be able to do this because you're loading a string and not a JSON object when you use Dart to read a JSON Object. Ultimately it would be complex to do, even if you add methods to the JSON object, Dart would still read everything as a string.我不认为你能做到这一点,因为当你使用 Dart 读取 JSON Object 时,你正在加载一个字符串而不是 JSON object。最终它会很复杂,即使你添加方法到JSON object、Dart 仍会将所有内容读取为字符串。 Then converts them to Dart objects when you use the jsonDecode method on the string .然后在对字符串使用jsonDecode方法时将它们转换为 Dart 对象。

you can not update a .json file directly.您不能直接更新.json文件。 use these steps instead:请改用以下步骤:

  1. create a dart file, for example globals.dart创建一个 dart 文件,例如globals.dart
  2. create your userJson file there:在那里创建你的 userJson 文件:
String userJson = json.encode(your json file);
  1. import globals in your code:在您的代码中导入全局变量:
import "./globals.dart" as globals;

List<User> users = json.decode(globals.userJson);

4.and then you can update the json parameter as follow: 4.然后您可以更新 json 参数如下:

updateJson() async {
    users[0]['companyName'] = "NewCompany";
    globals.userJson = json.encode(users);


  }
  1. for the extra steps, you can save this string (json.encoded file) in a DB or send it to the server.对于额外的步骤,您可以将此字符串(json.encoded 文件)保存在数据库中或将其发送到服务器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM