0

How to save JSON array into SharedPreferences?

I've tried some code like this below, but i got some error :

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'Text' is not a subtype of type 'String'

for (var x = 0; x < dynamicwidget.length - 1; x++) {
  _listOrder={
    "id_product": dataJSON[x]["product_id"],
    "order_count": dynamicwidget[x].controller.text,
  };
}

String json = jsonEncode(_listOrder);

SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(categoryname, json);
4
  • replace this dynamicwidget[x].controller.text with dynamicwidget[x].controller.text.toString() Commented May 6, 2019 at 6:35
  • Tks for your answer, but still not working Commented May 6, 2019 at 6:50
  • I think problem lies in this line only Commented May 6, 2019 at 7:03
  • instead of doing like this save order_count value in a variable and then assign it here Commented May 6, 2019 at 7:04

2 Answers 2

3

I would recommend you to use something like this (local storage implementation) which is made for saving and loading JSON.

From the code snippet itself i can't tell you exactly what is causing this exception but basically everywhere a String is beeing expected but you provided an actual Text widget. It could be the categoryname variable. What kind of controller do you get from your dynamicWidget instance? Are you sure controller.text returns a String?

Another note: you are re-assigning _listOrder with every for loop instead of adding information. You have to use an empty array which will be filled with every loop.

Sign up to request clarification or add additional context in comments.

3 Comments

Noted, Thank for you reference
this library is legacy as it doesn't support null safety
As of version 4.0.0+1 it has been made null safe and can therefore be used as usual
0

Thanks all, for contributing this question. I've solved this question and makes some change.

_listOrder.clear();
    for (var x = 0; x < dynamicwidget.length; x++) {
      int jml = int.parse(dynamicwidget[x].controller.text == ""? "0": dynamicwidget[x].controller.text);
      if (jml > 0) {
        _listOrder.add({
          "id_product": dataJSON[x]["product_id"],
          "order_count": dynamicwidget[x].controller.text,
        });
      }
    }

    if (_listOrder.length > 0) {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      Map<String, dynamic> _collOrder = Map<String, dynamic>();
      String sessjson = prefs.getString("orderlist");
      if (sessjson != null) {
        _collOrder = json.decode(sessjson);
        _collOrder.remove(categoryname);
      }
      _collOrder[categoryname] = _listOrder;
      prefs.setString("orderlist", json.encode(_collOrder));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.