这是因为GetImagePathFromSharedReferences不返回字符串?,它返回一个future
您可以在initState中处理异步任务,也可以使用FutureBuilder
make sure to set the return type of getImagePathFromSharedPreferences
Future<String?> getImagePathFromSharedPreferences() async {
// ...
}
我将使用FutureBuilder:
FutureBuilder<String?>(
future: getImagePathFromSharedPreferences(),
initialData: null,
builder: (
BuildContext context,
AsyncSnapshot<String?> snapshot,
) {
if (snapshot.connectionState != ConnectionState.done) {
return const CircularProgressIndicator();
} else if (!snapshot.hasError) {
return CircleAvatar(
radius: 45.0,
backgroundImage: _imageFile != null
? Image.file(_imageFile).image
: snapshot.hasData
? FileImage(snapshot.data!)
: AssetImage(_imageUrl),
);
} else {
return const Text('Error');
}
},
),