skip to Main Content

I’m reading data from my db , the specific data in question is a list which was recorded as a String in my database , now i’m trying to read the data by converting the String back into a List , how can i go about doing this wthe snippet of code below :

Widget build(BuildContext context) {
    return ref.watch(getDataByIdProvider(widget.ID)).when(
          data: (data) => Scaffold(
            body: ListView(
              children: [
                for (int i = 0; i < data.name.length; i++)//The list in question
                      RadioListTile(
                          title: Text(
                            data.name[i].toString(),
                          ),
                          value: i,
                          groupValue: _selectedRadio,
                          onChanged: (value) {
                            _selectedRadio = value as int?;
                          }),],

2

Answers


  1. if your string has been separated by ‘,’ character, you can try this way:

    Column(
              children: [
                ...data.name.split(',').map((e) => Text(e)).toList(),
              ],
            ),
    

    in this code, I have split my string by ‘,’ char, so dart lang will create a list of items before each ‘,’ char. you can turn Text widget to your desired one.

    Login or Signup to reply.
  2. One of the solutions is to store your list as a json string using jsonEncode and jsonDecode from dart:convert

    final list = ['one', 'to', 'three'];
    
    /// convert your List to json string
    final jsonString = jsonEncode(list);
    /// Call db.save(jsonString)
    
    /// Call db.get('yourJsonStringKey')
    /// Convert json string back to List
    final listFromJson = jsonDecode(jsonString);  
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search