Skip to content

[How to use] Ho to correctly load and show images using AssetEntity.fromId ? #1210

@vlavorini

Description

@vlavorini

Platforms

dart, Android, iOS, macOS

Description

What is the correct way to display images starting from the ID?

As example, with the following code I get the error:

The argument type 'Future<AssetEntity?>' can't be assigned to the parameter type 'AssetEntity'. dart[argument_type_not_assignable](https://dart.dev/diagnostics/argument_type_not_assignable)

And if I write await AssetEntity.fromId(retrEnt.localId) I get as error:

The await expression can only be used in an async function.

Can you help me?

P.S.
I already filed a SO question

My code

class AnalyzeMedia extends StatefulWidget {
  @override
  AnalyzeMediaState createState() {
    return AnalyzeMediaState();
  }
}


class AnalyzeMediaState extends State<AnalyzeMedia> {
  final _formKey2 = GlobalKey<FormState>();
  late Future<List<RetrievedMedia>> futureClusterSamples;


  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey2,
      child: Column(
        children: [
          ElevatedButton(
            /// doing something
          ),
          FutureBuilder<List<RetrievedMedia>>(
            future: futureClusterSamples,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                  return Column(
                    children: [
                      for (var retrEnt in snapshot.data!)
                        Image(
                          image: AssetEntityImageProvider(
                            await AssetEntity.fromId(retrEnt.localId),  // <--- HERE is the issue
                            isOriginal: false,
                            thumbnailSize: const ThumbnailSize.square(200),
                            thumbnailFormat: ThumbnailFormat.jpeg,
                          ),
                        )
                      
                    ],
                  );
              } else if (snapshot.hasError) {
                return Text('${snapshot.error}');
              }

              // By default, show a loading spinner.
              return const CircularProgressIndicator();
            },
          ),
          
        ],
      ),
    );
  }
}

Try do it

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions