Android 如何以编程方式将文本元素添加到我的小部件/类中?

Android 如何以编程方式将文本元素添加到我的小部件/类中?,android,ios,mobile,dart,flutter,Android,Ios,Mobile,Dart,Flutter,我有一个小部件,它调用函数从API获取数据;fetch函数完成后,它调用另一个函数为它的小部件构建一个表。代码如下: import 'package:flutter/material.dart'; import 'dart:convert'; import 'package:http/http.dart' as http; void main() => runApp(MeTube()); // class class MeTube extends StatefulWidge

我有一个小部件,它调用函数从API获取数据;fetch函数完成后,它调用另一个函数为它的小部件构建一个表。代码如下:

 import 'package:flutter/material.dart';
 import 'dart:convert';
 import 'package:http/http.dart' as http;
 void main() => runApp(MeTube());


 // class
 class MeTube extends StatefulWidget {
   @override
   State<StatefulWidget> createState() {
     return new MeTubeState();
   }
 }


 // state, component
 class MeTubeState extends State<MeTube> {
   bool _loading = true;

   @override
   Widget build(BuildContext context) {
     return MaterialApp(
       theme: ThemeData(
         // primarySwatch: Colors(212121),
       ),
       home: Scaffold(
         appBar: AppBar(
           title: Text('MeTube'),
           centerTitle: false,
           actions: <Widget>[
             IconButton(
               icon: Icon(Icons.refresh),
               onPressed: () {_fetch();},               // call the fetch function (refresh)
             )
           ],
         ),

         body: Center(
           child: _loading ? CircularProgressIndicator() : null   /* I could load the ListView here
                                                                   * and use the state to render each row
                                                                   * but if there are 1,000+ rows, that
                                                                   * would degrade the performance.
                                                                  */
         ),
       )
     );
   }


   // fetch data for the app
   _fetch() async {
     setState(() {_loading = true;});                   // start the progress indicator
     final String url = 'api.letsbuildthatapp.com/youtube/home_feed';
     final res = await http.get('https://$url');

     if (res.statusCode == 200) {                       // if successful, decode the json
       final map = json.decode(res.body);
       _build(map['videos'].length, map['videos']);     // pass the # videos, and the videos
     }
   }


   // build the data
   _build(int rows, data) {                /* MAKE EACH ROW */
     MeTube().createElement().build(       // create a ListView in the Class/Widget
       ListView.builder(
         itemCount: rows,                  /* number of rows to render, no need to setState() since this 
                                            * function (build) gets called, and the data is passed into it
                                           */

         itemBuilder: (context, index) {   // make each column for this class
           Column(children: <Widget>[
             Text(data[index]['name']),    // render some Text and a Divider in each row
             Divider()
           ]);
         },
       )
     );


     setState(() {_loading = false;});     // stop the progress indicator
   }
 }
导入“包装:颤振/材料.省道”;
导入“dart:convert”;
将“package:http/http.dart”导入为http;
void main()=>runApp(MeTube());
//阶级
类MeTube扩展了StatefulWidget{
@凌驾
状态createState(){
返回新的MeTubeState();
}
}
//状态、组件
类MeTubeState扩展状态{
bool_加载=真;
@凌驾
小部件构建(构建上下文){
返回材料PP(
主题:主题数据(
//主样本:颜色(212121),
),
家:脚手架(
appBar:appBar(
标题:文本(“MeTube”),
标题:错误,
行动:[
图标按钮(
图标:图标(Icons.refresh),
onPressed:(){u fetch();},//调用fetch函数(刷新)
)
],
),
正文:中(
child:_loading?CircularProgressIndicator():null/*我可以在这里加载ListView
*并使用状态渲染每一行
*但是如果有1000多行,那么
*会降低性能。
*/
),
)
);
}
//获取应用程序的数据
_fetch()异步{
setState((){u loading=true;});//启动进度指示器
最终字符串url='api.letsbuildthatpapp.com/youtube/home_feed';
final res=wait http.get('https://$url');
如果(res.statusCode==200){//如果成功,则解码json
final map=json.decode(res.body);
_构建(映射['videos'].长度,映射['videos']);//传递#视频和视频
}
}
//构建数据
_生成(int行,数据){/*生成每行*/
MeTube().createElement().build(//在类/小部件中创建ListView
ListView.builder(
itemCount:rows,/*要渲染的行数,因此不需要设置state()
*调用函数(build),并将数据传递到其中
*/
itemBuilder:(上下文,索引){//为此类创建每个列
栏(儿童:[
Text(data[index]['name']),//在每行中呈现一些文本和分隔符
分隔器()
]);
},
)
);
setState((){u loading=false;});//停止进度指示器
}
}

build()
函数中的当前代码非常简陋,并显示错误。如何以编程方式将ListView和行插入小部件,而不是将所有视频推送到状态,然后运行代码从状态中的所有这些值中呈现一行?

ListView.builder构造函数将在项目按需滚动到屏幕上时创建项目。我想你不必担心性能。就像你对代码的评论一样