“`dart Flutter: 在单个页面中依次显示两个FutureBuilder的数据 “`

huangapple go评论58阅读模式
英文:

Flutter: Display two FutureBuilders data one after another in a single page

问题

我有两个FutureBuilder数据需要在同一个页面上依次显示,即我想要将第二个FutureBuilder数据附加到第一个上。当我使用以下代码时,它将屏幕分为两个固定部分,上半部分显示第一个FutureBuilder数据,下半部分显示第二个FutureBuilder数据,并且两者可以分别滚动。如果我使用body: SingleChildScrollView而不是body: SafeArea,则结果输出是一个白屏(屏幕上没有数据),并在控制台中出现错误。

Failed assertion: line 2009 pos 12: 'hasSize' The relevant error-causing widget was Scaffold

以下是完整的程序:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class GetUserData extends StatefulWidget {
  const GetUserData({Key? key});

  @override
  State<GetUserData> createState() => _GetUserDataState();
}

class _GetUserDataState extends State<GetUserData> {
  Future<List<dynamic>> getUserData() async {
    String url = 'https://telugu-signaler.000webhostapp.com/get_user.php';
    var response = await http.post(Uri.parse(url), body: '');
    var allUserData = jsonDecode(response.body.toString());
    return allUserData;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Get User Data'),
      ),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 8,
              child: FutureBuilder<List<dynamic>>(
                future: getUserData(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: GridView.builder(
                        shrinkWrap: true,
                        gridDelegate:
                            const SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 1,
                          mainAxisExtent: 150.0,
                        ),
                        itemCount: snapshot.data!.length,
                        itemBuilder: (context, index) {
                          String userFirstName =
                              snapshot.data![index]['user_first_name'];
                          String userLastName =
                              snapshot.data![index]['user_last_name'];

                          return Column(
                            children: [
                              Container(
                                color: Colors.lightBlue,
                                child: Text(
                                  'First Name: $userFirstName',
                                  style: TextStyle(fontSize: 30),
                                ),
                              ),
                              Container(
                                color: Colors.lightBlue,
                                child: Text(
                                  'Last Name: $userLastName',
                                  style: TextStyle(fontSize: 30),
                                ),
                              ),
                            ],
                          );
                        },
                      ),
                    );
                  }
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            ),
            Expanded(
              flex: 8,
              child: FutureBuilder<List<dynamic>>(
                future: getUserData(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: GridView.builder(
                        shrinkWrap: true,
                        gridDelegate:
                            const SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,
                          mainAxisExtent: 150.0,
                        ),
                        itemCount: snapshot.data!.length,
                        itemBuilder: (context, index) {
                          String userDistrict =
                              snapshot.data![index]['user_district'];
                          String userState =
                              snapshot.data![index]['user_state'];

                          return Column(
                            children: [
                              Container(
                                color: Colors.lightGreen,
                                child: Text(
                                  'District: $userDistrict',
                                  style: TextStyle(fontSize: 30),
                                ),
                              ),
                              Container(
                                color: Colors.lightGreen,
                                child: Text(
                                  'State: $userState',
                                  style: TextStyle(fontSize: 30),
                                ),
                              ),
                            ],
                          );
                        },
                      ),
                    );
                  }
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

希望这对你有所帮助!

英文:

I have two FutureBuilder data to be displayed in a single page one after another i.e. I want to append second FutureBuilder data to first one. When I use the following code, it divides the screen into two fixed parts the upper half displays the first FutureBuilder data and the lower half displays the second FutureBuilder data and both are scrollable separately. If I use body:SingleChildScrollView instead body:SafeArea the resulting output is a white screen (no data on screen) with error in console

>Failed assertion: line 2009 pos 12: 'hasSize' The relevant error-causing widget was Scaffold

Here is the complete program:

import &#39;package:flutter/material.dart&#39;;
import &#39;package:http/http.dart&#39; as http;
import &#39;dart:convert&#39;;
class GetUserData extends StatefulWidget {
const GetUserData({super.key});
@override
State&lt;GetUserData&gt; createState() =&gt; _GetUserDataState();
}
class _GetUserDataState extends State&lt;GetUserData&gt; {
Future&lt;List&gt; getUserData() async {
String url = &#39;https://telugu-signaler.000webhostapp.com/get_user.php&#39;;
var response = await http.post(Uri.parse(url), body: &#39;&#39;);
var allUserData = jsonDecode(response.body.toString());
return allUserData;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(&#39;Get User Data&#39;),
),
body: SafeArea(
child: Column(
children: &lt;Widget&gt;[
Expanded(
flex: 8,
child: FutureBuilder&lt;List&lt;dynamic&gt;&gt;(
future: getUserData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: GridView.builder(
shrinkWrap: true, //add this line
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
mainAxisExtent: 150.0,
),
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
String userFirstName =
snapshot.data![index][&#39;user_first_name&#39;];
String userLastName =
snapshot.data![index][&#39;user_last_name&#39;];
return Column(
children: [
Container(
color: Colors.lightBlue,
child: Text(
&#39;First Name: $userFirstName&#39;,
style: TextStyle(fontSize: 30),
),
),
Container(
color: Colors.lightBlue,
child: Text(
&#39;Last Name: $userLastName&#39;,
style: TextStyle(fontSize: 30),
),
),
],
);
},
),
);
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
),
Expanded(
flex: 8,
child: FutureBuilder&lt;List&lt;dynamic&gt;&gt;(
future: getUserData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: GridView.builder(
shrinkWrap: true, //add this line
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisExtent: 150.0,
),
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
String userDistrict =
snapshot.data![index][&#39;user_district&#39;];
String userState =
snapshot.data![index][&#39;user_state&#39;];
return Column(
children: [
Container(
color: Colors.lightGreen,
child: Text(
&#39;District: $userDistrict&#39;,
style: TextStyle(fontSize: 30),
),
),
Container(
color: Colors.lightGreen,
child: Text(
&#39;State: $userState&#39;,
style: TextStyle(fontSize: 30),
),
),
],
);
},
),
);
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
),
],
),
),
);
}
}

答案1

得分: 0

你在同一弹性的列中扩展了2个小部件。所以屏幕分隔是正常的。你必须移除扩展的小部件。然后通过在GridView中添加physics: NeverScrollableScrollPhysics()来禁用滚动。接着将SafeArea更改为SingleChildScrollView。

英文:

You expanded 2 widgets in column with same flex. So it's normal to split screen. You have to remove Expanded widgets. Then disable scrolling on GridViews by adding physics: NeverScrollableScrollPhysics() to GridView. Then change the SafeArea with SingleChildScrollView.

huangapple
  • 本文由 发表于 2023年6月22日 08:15:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定