简体   繁体   中英

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

I am using a local JSON file in my flutter app. On a button click I want to change one item in same JSON file. The JSON file (UserData.json) is:

        {
            "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:

  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. 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

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. Then converts them to Dart objects when you use the jsonDecode method on the string .

you can not update a .json file directly. use these steps instead:

  1. create a dart file, for example globals.dart
  2. create your userJson file there:
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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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