【问题标题】:Retrieve data stored in SQFlite database (Flutter)?检索存储在 SQFlite 数据库 (Flutter) 中的数据?
【发布时间】:2019-12-03 10:06:54
【问题描述】:

我有一个模型类Pet 使用toMapfromMap

  Map<String, dynamic> toMap() {
return {
  'id': id,
  'image': image,
  'name': name,
  'birth': birth,
  'colour': colour,
  'breed': breed,
  'hairlength': hairLength,
  'sterilized': sterilized,
  'vaccinated': vaccinated
   };
  }

    Pet.fromMap(Map<String, dynamic> map) {
    id = map['id'];
    image = map['image'];
    name = map['name'];
    birth = map['birth'];
    breed = map['breed'];
    colour = map['colour'];
    hairLength = map['hairlength'];
    sterilized = map['sterilized'];
    vaccinated = map['vaccinated'];
  }

我将所有内容都存储在我的数据库中,并且我想检索图像(存储为字符串),所以我使用了这种方法:

  Future<List<String>> getImages() async {
    var dbClient = await db;
    List<Map> maps = await dbClient.rawQuery("SELECT * FROM $TABLE");
    List<String> images = [];
    if (maps.length > 0) {
       for (int i = 0; i < maps.length; i++) {
           images.add(Pet.fromMap(maps[i]).image);
      }
     }
    return images;
  }

如何使用Future&lt;List&lt;String&gt;&gt; 读取与每个图像关联的每个字符串?

【问题讨论】:

标签: string image sqlite flutter


【解决方案1】:

问题有点模糊,假设像评论中提到的那样,您想直接在您的小部件中使用该函数,您可以使用 FutureBuilder 或在完成后在 initstate 和 setState 中调用该函数。

FutureBuilder 版本

class TextFutureWidget extends StatelessWidget {
  Future<List<String>> testFunction() async {
    return List<String>();
  }

  @override
  Widget build(BuildContext context) {
    final imagesFuture = testFunction();
    return Container(
        child: FutureBuilder(
      future: imagesFuture,
      builder: (context, snapshot) {
        if (!snapshot.hasData || snapshot.data == null) return Container();
        return Column(
          children: <Widget>[
            for (var item in snapshot.data) Text(item),
          ],
        );
      },
    ));
  }
}

初始化版本

class TestWidget extends StatefulWidget {
  @override
  _TestWidgetState createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {
  List<String> _images;
  List<String> get images => _images;
  set images(List<String> images) {
    if (mounted) setState(() => _images = images);
  }

  @override
  void initState() {
    super.initState();
    testFunction().then((value) => images = value);
  }

  Future<List<String>> testFunction() async {
    return List<String>();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        if(images != null)
          for (var item in images)
            Text(item),
      ],
    );
  }
}

【讨论】:

  • 使用 Initstate 版本方法,我设置 _images = dbHelper.getImages(); ?
  • testFunction().then((value) => images = value);用你的获取图像和图像替换函数 _images
  • @EdoardoTavilla 如果问题解决了您的问题,请将其标记为关闭问题的答案。
【解决方案2】:

你可以这样使用

 body: new Container(
    padding: new EdgeInsets.all(10.0),
    child: new FutureBuilder<List<Teams>>(
      future: fetchTeamListFromDatabase(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return new ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (context, index) {
                decoImage = snapshot.data[index].teamFlag;
                _bytesImage = Base64Decoder().convert(decoImage);

          
               
                    child: Card(

                       elevation: 2.0,
                      clipBehavior: Clip.antiAliasWithSaveLayer,
                      semanticContainer: false,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(5.0),
                      ),
                      
                      margin: EdgeInsets.all(5),
                      color: Colors.white,
                    child: ListTile(
                    //  child: Row(children: <Widget>[
                       leading: CircleAvatar(
                         radius: 26,
                          backgroundColor: Colors.white,
                      child:  ClipOval(
                          child: _bytesImage == null
                              ? new Text('No image value.')
                              : Image.memory(
                                  _bytesImage,
                                  width: 60,
                                  height: 60,
                                  fit: BoxFit.cover,
                                ),
                        ),
                       ),
                       
                     
                     // ]
                      ),
                    ),
                //  ),
                );
              });
        } else if (snapshot.hasError) {
          return new Text("No teams available yet");
        }
        return new Container(
          alignment: AlignmentDirectional.center,
          child: new CircularProgressIndicator(),
        );
      },
    ),
  ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2021-11-22
    • 2021-07-09
    • 2020-04-13
    • 2019-04-07
    相关资源
    最近更新 更多