- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 api 获取新闻数据并在主页中显示新闻标题据我所知,它不会有任何内部变化,所以我决定使用 StatelessWidget 以及使用 PROVIDER 状态管理现在我遇到了如何使用 StatelessWidget 调用获取方法以便显示标题的问题
这是我获取数据的类
class NewsRequest with ChangeNotifier{
static String _url ="https://newsapi.org/v2/everything?q=bitcoin&from=2019-06-24&sortBy=publishedAt&apiKey=4d990bdd71324572bca39fe31edc3990";
static Map<String, String> _apiKey = {"apiKey" : "4d990bdd71324572bca39fe31edc3990"};
Map <String, dynamic> _data;
List _articles;
bool _isFetching = false;
Map<String, dynamic> result;
bool get isFetching => _isFetching;
Future<Map<String, dynamic>> fetchData() async{
_isFetching = true;
try{
Response response = await get(Uri.encodeFull(_url), headers: _apiKey)
.timeout(Duration(seconds: 60));
print("STATUSCODE ${response.statusCode}");
if(response.statusCode == 200){
_data = json.decode(response.body);
}
_isFetching = false;
notifyListeners();
}on SocketException catch(_){
}on TimeoutException catch(_){
}catch(e){
e.toString();
print('CATCH ${e.toString()}');
}
return null;
}
Map<String, dynamic> get getNews => _data;
Map<String , dynamic> getNewsData(){
if(_data == null){
print('data is null');
return null;
}else{
_articles = _data['articles'];
}
print("FIRST ARTICALE IS : ${_articles[0]}");
return null;
}
}
我的主页调用是
body: newsResponse.isFetching
? Center(
child: CircularProgressIndicator(),
)
: newsResponse.getNewsData()!= null ?
new ListView.builder(
itemCount: newsResponse.getNewsData().length,
itemBuilder: (BuildContext context, int index) {
return new Container(
padding: EdgeInsets.all(10),
child: Stack(
children: <Widget>[
Container(
height: 100,
width: 310,
),
child: Wrap(children: <Widget>[
Text( newsResponse.result['response'][index]['title']),
]),
),
CircleAvatar(
radius: 50,
backgroundImage: NetworkImage(
newsResponse.result['response'][index]["urlToImage"]??"",
),
),
],
),
);
},
):
Container()
我需要调用fetchData() 方法来运行所有人员
最佳答案
您可以直接在ChangeNotifier
的构造函数中启动请求:
class MyNotifier with ChangeNotifer {
MyNotifier() {
// TODO: do an http request
}
}
关于flutter - 如何使用 statelessWidget 从 Api 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57183446/
当我在 flutter 中创建一个简单、基本的应用程序(如下面的应用程序)时,我创建了一个非显式 StatelessWidget? import 'package:flutter/material.d
在 StatelessWidget 中为 ListView 使用 ScrollController 是否可以。如果可以, Controller 如何处理? 最佳答案 如果您使用的是 ScrollCon
我是 Flutter 的新手,但仍然不知道如何以正确的方式做事。我希望标题足够清楚,因为我不知道解决这个问题的正确关键字。第一个片段扩展了 StatelessWidget: class Floatin
我想用 ListView 和 JSON 显示数据列表。此代码在一个文件中,带有 stateless widget。登录后打开此页面。当我在 stateful widget 中尝试时,代码运行正常。在
我想用 ListView 和 JSON 显示数据列表。此代码在一个文件中,带有 stateless widget。登录后打开此页面。当我在 stateful widget 中尝试时,代码运行正常。在
我将 redux 用于 flutter,因此每个演示文稿都是无状态的,但我无法让下拉菜单工作属性,因为我无法更改它的状态以在从下拉列表中选择值时触发 flutter 来重建下拉部分。 我正在尝试为
StatelessWidget 的所有代码都在一个类中,甚至包括 build 方法。对于 StatefulWidget,为什么 State 是一个独立于 Widget 的类?不能将它们组合起来,Sta
我注意到,我们可以使用 StatefulWidget 构建 UI 的任何部分,而无需担心状态。 我想问的很简单,当我们可以将任何 UI 类构建为 StatefulWidget 时,首先拥有 State
我有一个简单的 StatelessWidget,它在其构造函数中创建了一个新的 StatefulWidget。问题是要创建那个小部件,需要先创建另一个对象。然后当我将它传递给构造函数时,出现错误 错误
我在 flutter 中有以下代码。在 initialRoute:属性,需要调用isLoggedIn()方法,它是一个异步函数。我收到一条错误消息,提示我需要调用 await在 async功能。但是
是否有反对使用 final foo = Builder(builder: (BuildContext context) { ... }); 代替 class Foo e
我想知道如果我定义一个 StatelessWidget 但从它的构建方法返回一个有状态的 Widget 会发生什么?我试过了,一切似乎都正常,但我只想知道幕后发生了什么,这样我就可以确保在将它投入生产
我试着这样写 showModalBottomSheet( context: context, // I got error here (Undefined name 'context'.dart
在性能方面,使用 StatelessWidget 与 返回 Widget 的函数 有什么区别吗? 我至少清楚 flutter's repo issue 中指出的差异与性能无关。 事实上,我有一些同事声
我知道 StatefulWidget 允许我们更改小部件,而 StatelessWidget 不允许。 但是如果 StatefulWidget 可以执行 StatelessWidget 可以执行的所有
这个问题在这里已经有了答案: How to deal with unwanted widget build? (6 个回答) 关闭3年前。 我总是将代码放在我的问题中,但这次是不可能的,因为错误可能存
如果我使用的是 StatefulWidget,那么我将在 initState 方法中监听 Stream。我会在 StatelessWidget 中哪里做同样的事情(比如使用 Bloc 和流进行状态管理
我创建了简单的 PageView 应用程序来测试多个页面。 import 'package:flutter/material.dart'; void main() => runApp(MyApp())
我正在尝试从 api 获取新闻数据并在主页中显示新闻标题据我所知,它不会有任何内部变化,所以我决定使用 StatelessWidget 以及使用 PROVIDER 状态管理现在我遇到了如何使用 Sta
我正在尝试从 api 获取新闻数据并在主页中显示新闻标题据我所知,它不会有任何内部变化,所以我决定使用 StatelessWidget 以及使用 PROVIDER 状态管理现在我遇到了如何使用 Sta
我是一名优秀的程序员,十分优秀!