gpt4 book ai didi

android-studio - Flutter App不会显示内容,除非单击某些按钮

转载 作者:行者123 更新时间:2023-12-03 04:35:17 25 4
gpt4 key购买 nike

因此,我的flutter应用程序不会显示通过API提取的内容,除非按下某些按钮然后仅显示它,
我不知道这里是什么问题,但这很烦人,
我不知道是什么原因,因此下面是我的整个代码,
我是新来的人,所以很抱歉,如果这是一个愚蠢的错误。
谢谢。

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:FotoApp/image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter_downloader/flutter_downloader.dart';

final Color myColor = Color(0xff222f3e);
final Color myColor2 = Color(0xff2f3640);

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterDownloader.initialize(debug: true);
runApp(
MyApp(),
);
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
static String query = "wallpapers";
List<dynamic> wallpapersList;
Icon searchIcon = Icon(Icons.search);
Widget searchBar = Text("FotoApp");

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

void initialize() async {
var apiUrl =
"https://api.pexels.com/v1/search?query=" + query + "&per_page=500";
http.Response response = await http.get(
apiUrl,
headers: {
HttpHeaders.authorizationHeader:
"563492ad6f91700001000001999da5bd71d04ece9af9ba1a03e8beaf"
},
);
print(apiUrl);
if (response.statusCode == 200) {
try {
final responseJson = jsonDecode(response.body);
wallpapersList = responseJson['photos'];
} catch (e) {
print(e);
}
} else
print(response.reasonPhrase);
}

@override
Widget build(BuildContext context) {
var tabindex = 0;

return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: searchBar,
backgroundColor: myColor,
actions: [
IconButton(
icon: searchIcon,
onPressed: () {
setState(() {
if (this.searchIcon.icon == Icons.search) {
this.searchIcon = Icon(Icons.cancel);
this.searchBar = TextField(
textInputAction: TextInputAction.go,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search",
hintStyle: TextStyle(color: Colors.white)),
onSubmitted: (value) {
query = value;
initialize();
print(query);
},
);
} else {
this.searchIcon = Icon(Icons.search);
this.searchBar = Text("FotoApp");
}
});
},
color: Colors.white,
)
],
),
body: wallpapersList != null
? StaggeredGridView.countBuilder(
padding: const EdgeInsets.all(8.0),
crossAxisCount: 4,
itemBuilder: (BuildContext context, int index) {
String imgPath = wallpapersList[index]["src"]["large"];
return Card(
child: InkWell(
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImagePath(imgPath))),
child: Hero(
tag: imgPath,
child: FadeInImage(
width: MediaQuery.of(context).size.width,
placeholder: AssetImage("assets/loading.gif"),
image: NetworkImage(imgPath),
fit: BoxFit.cover,
)),
),
);
},
staggeredTileBuilder: (index) =>
StaggeredTile.count(2, index.isEven ? 2 : 3),
itemCount: wallpapersList.length,
)
: Center(
child: CircularProgressIndicator(
backgroundColor: Colors.white,
),
),
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.black,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.grey,
currentIndex: tabindex,
type: BottomNavigationBarType.shifting,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.image), title: Text("Wallpapers")),
BottomNavigationBarItem(
icon: Icon(Icons.view_list), title: Text("Categories")),
BottomNavigationBarItem(
icon: Icon(Icons.info), title: Text("About"))
],
),
backgroundColor: myColor2,
),
);
}
}

最佳答案

替换为:

try {
final responseJson = jsonDecode(response.body);
wallpapersList = responseJson['photos'];
} catch (e) {
print(e);
}
有了这个:
try {
final responseJson = jsonDecode(response.body);
setState(() {
wallpapersList = responseJson['photos'];
});
} catch (e) {
print(e);
}

关于android-studio - Flutter App不会显示内容,除非单击某些按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64228433/

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