我正在编写一个简单的应用程序,其中有一个“故事”部分,其中显示多个用户,并在其图片下显示其用户名。为了生成随机图片,直到我拥有任何类型的数据库为止,我都在使用pravatar,当我在浏览器中打开该数据库时,每次我重新加载时都会得到不同的图片。但是,在Flutter中生成多个这些化身时,每次我使用https://i.pravatar.cc/150获得一个新的NetworkImage时,它都会返回相同的图像。然后,我得到了一群拥有相同图片的用户。我不是要主张这项权利吗?它只获取一次图像吗?这是我的代码:
for (int i = 0; i < users.length; ++i) {
activeFriends.add(getUserWidget(users[i]));
}
其中的getUserWidget()运行以下代码:
Container(
child: CircleAvatar(
radius: 30,
backgroundImage: NetworkImage('https://i.pravatar.cc/150'),
backgroundColor: Colors.transparent,
),
),
答案 0 :(得分:0)
图像已缓存NetworkImage is caching the old image
pravatar.cc接受带有https://i.pravatar.cc/150?img=3的直接图像ID
对于您的情况,您可以将索引作为图像ID
代码段
Container(
child: CircleAvatar(
radius: 30,
backgroundImage: NetworkImage('https://i.pravatar.cc/150?img=1'),
backgroundColor: Colors.transparent,
),
),
工作演示
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: CircleAvatar(
radius: 30,
backgroundImage: NetworkImage('https://i.pravatar.cc/150?img=1'),
backgroundColor: Colors.transparent,
),
),
Container(
child: CircleAvatar(
radius: 30,
backgroundImage: NetworkImage('https://i.pravatar.cc/150?img=4'),
backgroundColor: Colors.transparent,
),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}