skip to Main Content

I’m using Drift to manage databases in my flutter app and I need to get a list from a database. When I call for a list it just returns something called a "Instance of ‘_MappedSelectable<QueryRow, String>’"

This is the SQL command that returns this getAllNicks: SELECT nickname FROM car_entries;

I’ve tried to convert it to a list a few ways but they all just turn up in errors. Here is one way i tried (DIDN’T WORK)


List<String> convertInstanceToList(instance) {
  Map<String, dynamic> map = Map.from(instance);
  List<String> stringList = map.values.toList().cast<String>();
  return stringList;
  }


I was hoping this would convert it to a map and then turn that into a usable list but I just get this error.

Class '_MappedSelectable<QueryRow, String>' has no instance method 'toList'.
Receiver: Instance of '_MappedSelectable<QueryRow, String>'
Tried calling: toList()

Anyone got anything that might work?

2

Answers


  1. Chosen as BEST ANSWER

    Using a snapshot on the future also seems to work in some cases.


  2. You need a Converter:

    import 'dart:convert';
    
    import 'package:drift/drift.dart';
    
    class StringListTypeConverter extends TypeConverter<List<String>, String> {
      @override
      List<String> fromSql(String fromDb) {
        return List<String>.from(json.decode(fromDb));
      }
    
      @override
      String toSql(List<String> value) {
        return json.encode(value);
      }
    }
    

    And you can use this converter in your table definition:

    @drift.UseRowClass(MyCustomRowClass, generateInsertable: true)
    class CommandsExecute extends Table{
      .
      .
      .
      drift.TextColumn get myStringList => text().map(StringListTypeConverter())();
    }
    

    And a custom model:

    JsonSerializable()
    class MyCustoRowClass extends Insertable<AssemblyMember> {
    List<String> myStringList;
    MyCustomRowClass({required this.myStringList});
    
    .
    .
    .
    //fromJson and toColumns methods
    }
    
    

    In this way you will be able to use a String list anywhere in your code:

    List<String> myList = myCustomRowClass.myStringList;
    

    I hope this can help you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search