gpt4 book ai didi

dart - 我无法添加 Text(installedApps[index] ["app_name"]) 因为 'index' 未定义。我怎样才能添加这个文本?

转载 作者:IT王子 更新时间:2023-10-29 06:59:08 26 4
gpt4 key购买 nike

我正在构建一个 flutter 应用程序。我已经构建了一个带有构造函数的类。我制作了构造函数,因此我可以自定义我的 ListTile

像这样:

class AppList extends StatefulWidget {

@override
AppListState createState() => AppListState();

AppList({Key key, this.child}) : super(key: key);

final Widget child;

}

class AppListState extends State<AppList> {

Widget child;

List<Map<String, String>> _installedApps;

@override
void initState() {
super.initState();
}

getApps() {
setState(() {
installedApps = _installedApps;
getApp();
});
}

@override
Widget build(BuildContext context) {

if (installedApps == null)
getApps();

return ListView.builder(
itemCount: installedApps == null ? 0 : installedApps.length,
itemBuilder: (context, index) {
return child; //This is where the ListTile will go.
},
);
}

}

//Just in case you were confused, I used a plugin for some of the features

在我构建这个类之后,我将它放在我的示例类中。

示例类:

class Example extends StatefulWidget {

@override
ExampleState createState() => ExampleState();

}

class ExampleState extends State<Example> {

@override
Widget build(BuildContext context) {
return MaterialApp (
debugShowCheckedModeBanner: false,
home: Scaffold (
body: Container (
color: Colors.black,
child: AppList (
child: ListTile (
title: Text(installedApps[index]["app_name"]) //this is the text
),
)
)
),
);
}

}

然后,我在其中添加了一个 ListTile 和一个文本。但是当我在写文字时,我意识到我无法输入我想要的文字。这是因为 Example 类中没有定义 'index'。

有什么好的方法可以将此文本放入我的示例类中吗?

完整代码:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'dart:async';
import 'dart:io';

import 'package:flutter_appavailability/flutter_appavailability.dart';

void main() {
SystemChrome.setEnabledSystemUIOverlays([]);
runApp(Example());
}

Future<void> getApp() async {

if (Platform.isAndroid) {

installedApps = await AppAvailability.getInstalledApps();

print(await AppAvailability.checkAvailability("com.android.chrome"));

print(await AppAvailability.isAppEnabled("com.android.chrome"));

}
else if (Platform.isIOS) {
installedApps = iOSApps;

print(await AppAvailability.checkAvailability("calshow://"));

}

}

List<Map<String, String>> installedApp;
List<Map<String, String>> installedApps;
List<Map<String, String>> iOSApps = [
{
"app_name": "Calendar",
"package_name": "calshow://"
},
{
"app_name": "Facebook",
"package_name": "fb://"
},
{
"app_name": "Whatsapp",
"package_name": "whatsapp://"
}
];

class Example extends StatefulWidget {

@override
ExampleState createState() => ExampleState();

}

class ExampleState extends State<Example> {

@override
Widget build(BuildContext context) {
return MaterialApp (
debugShowCheckedModeBanner: false,
home: Scaffold (
body: Container (
color: Colors.black,
child: AppList ()
)
),
);
}

}

class AppList extends StatefulWidget {

@override
AppListState createState() => AppListState();

AppList({Key key, this.child}) : super(key: key);

final Widget child;

}

class AppListState extends State<AppList> {

Widget child;

List<Map<String, String>> _installedApps;

@override
void initState() {
super.initState();
}

getApps() {
setState(() {
installedApps = _installedApps;
getApp();
});
}

@override
Widget build(BuildContext context) {

if (installedApps == null)
getApps();

return ListView.builder(
itemCount: installedApps == null ? 0 : installedApps.length,
itemBuilder: (context, index) {
return ListTile (
title: Text(installedApps[index]["app_name"])
);
},
);
}

}

最佳答案

为什么不将 ListTile 小部件放入 ListView 构建器中?

像这样:

return ListView.builder(
itemCount: installedApps == null ? 0 : installedApps.length,
itemBuilder: (BuildContext context, int index){
return ListTile(
title: Text(installedApps[index]["app_name"])
);
)

因为我怀疑即使您获得了索引,您仍然必须找到一种方法来从您的 AppListState 类访问您的 installedApps 变量到您的 ExampleState 类。

您可能还想在 getApps() 方法之外声明 installedApps 变量,就像您在 _installedApps 变量中所做的那样。像这样:

List<Map<String, String>> installedApps;

最后,getApp() 函数的作用是什么?您的 _installedApps 变量如何获取数据?

更新

我修改了您的代码并使用了 FutureBuilder 小部件。

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:flutter_appavailability/flutter_appavailability.dart';

void main() {
SystemChrome.setEnabledSystemUIOverlays([]);
runApp(Example());
}


class Example extends StatelessWidget {

@override
Widget build(BuildContext context) {
return MaterialApp (
debugShowCheckedModeBanner: false,
home: Scaffold (
body: Container (
color: Colors.white,
child: AppList ()
)
),
);
}

}


class AppList extends StatefulWidget {

AppList({Key key}) : super(key: key);

@override
AppListState createState() => AppListState();

}

class AppListState extends State<AppList> {

//List<Map<String, String>> installedApp;
List<Map<String, String>> installedApps;
List<Map<String, String>> iOSApps = [
{
"app_name": "Calendar",
"package_name": "calshow://"
},
{
"app_name": "Facebook",
"package_name": "fb://"
},
{
"app_name": "Whatsapp",
"package_name": "whatsapp://"
}
];


Future<List<Map<String, String>>> getApp() async {

if (Platform.isAndroid) {

installedApps = await AppAvailability.getInstalledApps();

print(await AppAvailability.checkAvailability("com.android.chrome"));

print(await AppAvailability.isAppEnabled("com.android.chrome"));

}
else if (Platform.isIOS) {
installedApps = iOSApps;

print(await AppAvailability.checkAvailability("calshow://"));

}

return installedApps;
}


@override
Widget build(BuildContext context) {

return FutureBuilder(
future: getApp(),
builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot){

switch(snapshot.connectionState){
case ConnectionState.none:
return Text('None');
case ConnectionState.active:
case ConnectionState.waiting:
return Text('Awaiting result...');
case ConnectionState.done:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
if (!snapshot.hasData)
return Text('No Data');

return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile (
title: Text(snapshot.data[index]["app_name"])
);
},
);
}
}
);
}
}

这是我的 Android 手机上的输出: enter image description here

关于dart - 我无法添加 Text(installedApps[index] ["app_name"]) 因为 'index' 未定义。我怎样才能添加这个文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53994073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com