Search code examples
flutterdartriverpod

How to convert string from database read to List<String>


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?;
                          }),],

Solution

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