快照数据null但FutureBuilder返回数据?
我有http响应数据,但它是零????? ...
Future GetCategoryImage()async { var url =“ http://172.17.40.225/shoplacviet/getCategoryImage.php”; var reponse =等待http.get(uri.parse(url)); var List = Reponse.body; uint8list _bytesimage; _bytesimage = base64decoder()。convert(list); 返回_bytesimage; }
...
FutureBuilder(
future: getcategoryimage(),
builder: (context,snapshot){
List lista = snapshot.data as List;//------------> I have http response data but IT IS NULL?????
if(snapshot.hasError) print(snapshot.error);
return snapshot.hasData ? ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: lista.length,
itemBuilder: (context,index){
var blob = lista[index]['categoryimage'];
Uint8List _bytesImage;
_bytesImage = Base64Decoder().convert(blob);
return Container(
child: Image.memory(_bytesImage),
);
}):Center(child: CircularProgressIndicator(),) ;
},
),
I have http response data but IT IS NULL?????
...
Future getcategoryimage() async{
var url = "http://172.17.40.225/shoplacviet/getcategoryimage.php";
var reponse = await http.get(Uri.parse(url));
var list = reponse.body;
Uint8List _bytesImage;
_bytesImage = Base64Decoder().convert(list);
return _bytesImage;
}
...
FutureBuilder(
future: getcategoryimage(),
builder: (context,snapshot){
List lista = snapshot.data as List;//------------> I have http response data but IT IS NULL?????
if(snapshot.hasError) print(snapshot.error);
return snapshot.hasData ? ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: lista.length,
itemBuilder: (context,index){
var blob = lista[index]['categoryimage'];
Uint8List _bytesImage;
_bytesImage = Base64Decoder().convert(blob);
return Container(
child: Image.memory(_bytesImage),
);
}):Center(child: CircularProgressIndicator(),) ;
},
),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在可用之前,请勿访问
数据
。使用hasdata
和haserror
属性类似:Do not access
data
before it is available. UsehasData
andhasError
properties something like this:您将未来构建为
未来:
参数<代码>未来构建器。由于这是在build()
方法中,因此您的未来可能会重置每秒60次。适当的策略(根据所以你有。将您的呼叫移至
getCategoryImage()
将其移入initstate()
,将其保存到state
变量中。我在
You're building the future as the
future:
argument to yourFutureBuilder
. Since this is in abuild()
method, your future might be getting reset up to 60 times per second. The proper strategy (according to the first few paragraphs of the documentation) is:So there you have it. Move your call to
getcategoryimage()
out intoinitState()
, saving it into aState
variable.I illustrate this in a 10-minute video, if you need further clarification.