Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 检索存储在SQFlite数据库中的数据(颤振)?_String_Image_Sqlite_Flutter - Fatal编程技术网

String 检索存储在SQFlite数据库中的数据(颤振)?

String 检索存储在SQFlite数据库中的数据(颤振)?,string,image,sqlite,flutter,String,Image,Sqlite,Flutter,我有一个模型类宠物用于映射和fromMap: 我将所有内容存储在数据库中,并希望检索存储为字符串的图像,因此我使用了以下方法: Future<List<String>> getImages() async { var dbClient = await db; List<Map> maps = await dbClient.rawQuery("SELECT * FROM $TABLE"); List<String> ima

我有一个模型类宠物用于映射和fromMap:

我将所有内容存储在数据库中,并希望检索存储为字符串的图像,因此我使用了以下方法:

  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来读取与每个图像关联的每个字符串?

问题有点含糊不清,假设您想在小部件中直接使用该函数,您可以使用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),
      ],
    );
  }
}

你可以这样使用

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

您正在寻找吗?使用Initstate version方法,其中我设置了_images=dbHelper.getImages?testFunction.thenvalue=>images=value;将函数替换为“获取图像”和“使用图像”_images@EdoardoTavilla如果问题解决了您的问题,请将其标记为答案以结束问题。
 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(),
        );
      },
    ),
  ),