快照数据null但FutureBuilder返回数据?

发布于 2025-02-11 07:57:47 字数 1100 浏览 2 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2025-02-18 07:57:47

在可用之前,请勿访问数据。使用hasdatahaserror属性类似:

FutureBuilder<future type>(
  future: _future, // a previously-obtained Future
  builder: (BuildContext context, AsyncSnapshot<future type> snapshot) {
    if (snapshot.hasData) {
      // here snapshot.data is available
      return <hasData widget>
    } else if (snapshot.hasError) {
      return <hasError widget>
    } else {
      return <waiting widget>
    }
  }
)

Do not access data before it is available. Use hasData and hasError properties something like this:

FutureBuilder<future type>(
  future: _future, // a previously-obtained Future
  builder: (BuildContext context, AsyncSnapshot<future type> snapshot) {
    if (snapshot.hasData) {
      // here snapshot.data is available
      return <hasData widget>
    } else if (snapshot.hasError) {
      return <hasError widget>
    } else {
      return <waiting widget>
    }
  }
)
日裸衫吸 2025-02-18 07:57:47

您将未来构建为未来:参数<代码>未来构建器。由于这是在build()方法中,因此您的未来可能会重置每秒60次。适当的策略(根据

未来必须是早些时候获得的,例如在状态期间。在构造未来构建器时,不得在状态期间创建它。Build或NectelessWidget.build方法调用。如果未来与FutureBuilder同时创建未来,那么每次重建FutureBuilder的父母时,异步任务都会重新启动。
一般指南是假设每个构建方法都可以被调用每个帧,并将省略的调用视为优化。

所以你有。将您的呼叫移至getCategoryImage()将其移入initstate(),将其保存到state变量中。

我在

You're building the future as the future: argument to your FutureBuilder. Since this is in a build() 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:

The future must have been obtained earlier, e.g. during State.initState, State.didUpdateWidget, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted.
A general guideline is to assume that every build method could get called every frame, and to treat omitted calls as an optimization.

So there you have it. Move your call to getcategoryimage() out into initState(), saving it into a State variable.

I illustrate this in a 10-minute video, if you need further clarification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文