English 中文(简体)
• 气溶胶中的二氧化二氮
原标题:Can t consume API with DIO in Flutter

我试图与德国国际交易日志一道消费APIC端点,但没有显示任何数据。 当我与邮政员一起尝试时,终点站的工作是正确的,但我不敢肯定什么错误。 我的守则如下:

import  package:dio/dio.dart ;
import  package:flutter/material.dart ;
import  package:tes2/article.dart ;

class ArticleProvider extends ChangeNotifier {
  List<Article> articles = [];
  final Dio dio = Dio();

  Future<List<Article>> getAllArticles() async {
    try {
      const String url =  https://13.210.163.192:8080/users/public/articles ;
      Response response = await dio.get(
        url,
      );
      articles = (response.data[ data ][ article ] as List)
          .map((e) => Article.fromJson(e))
          .toList();

      return articles;
    } catch (e) {
      rethrow;
    }
  }
}

class Article {
  String? id;
  String? image;
  String? title;
  String? author;
  String? topic;
  int? viewCount;
  int? commentCount;
  String? date;
  bool? saved;

  Article({
    this.id,
    this.image,
    this.title,
    this.author,
    this.topic,
    this.viewCount,
    this.commentCount,
    this.date,
    this.saved,
  });

  Article.fromJson(Map<String, dynamic> json) {
    id = json[ id ];
    image = json[ image ];
    title = json[ title ];
    author = json[ author ];
    topic = json[ topic ];
    viewCount = json[ view_count ];
    commentCount = json[ comment_count ];
    date = json[ date ];
    saved = json[ saved ];
  }
}

class ArticleListScreen extends StatelessWidget {
  const ArticleListScreen({Key? key});

  @override
  Widget build(BuildContext context) {
    // Register ArticleProvider as a provider above the widget tree
    final articleProvider = Provider.of<ArticleProvider>(context);

    // Execute fetchArticles when the widget is built
    articleProvider.getAllArticles();

    return Scaffold(
      appBar: AppBar(
        title: const Text( Daftar Artikel ),
      ),
      body: Consumer<ArticleProvider>(
        builder: (context, articleProvider, _) {
          // Check if the article data has been retrieved
          if (articleProvider.articles.isEmpty) {
            return const Center(child: CircularProgressIndicator());
          }

          // Display the article data
          return ListView.builder(
            itemCount: articleProvider.articles.length,
            itemBuilder: (context, index) {
              return ListTile(
                leading:
                    Image.network(articleProvider.articles[index].image ??   ),
                title: Text(articleProvider.articles[index].title ??   ),
                subtitle: Text(articleProvider.articles[index].author ??   ),
              );
            },
          );
        },
      ),
    );
  }
}

谁能帮助我? 我 st着这一错误,我已经尝试过许多辅导员,但似乎没有他们工作。 http://13.210.163.192:80/users/public/articles。 谢谢。

问题回答

需添加notificationListeners();

增 编

  articles = (response.data[ data ][ article ] as List)
      .map((e) => Article.fromJson(e))
      .纽约总部List();
  // This will notify listeners and cause re-render.
  notifyListeners();
  return articles;

But in your current code this may cause recursive calls. so also change the following code

final articleProvider = Provider.of<ArticleProvider>(context);

纽约总部

final articleProvider = Provider.of<ArticleProvider>(context,listen: false);

Edit:
I don t know where are you creating the provider. Wherever you are creating it, make sure it s similar 纽约总部 this pattern

  runApp(
    MultiProvider(providers: [
      ChangeNotifierProvider(create: (_)=> ArticleProvider())
    ],
      child: const MyApp(),
    )
  );




相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

getting XML from other domain using ASP.NET

I m fairly new to ASP.NET. And I was wondering how I could go about getting xml from a site (Kuler s API in this case), and then post the result using AJAX? So what I want here, is to be able to do a ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

UML Diagram to Model API

I need to create a diagram to document a RESTFul API that build, which UML diagram should I use? Thanks in advance,

How best to expose Rails methods via an API?

Let s say I have a model foo, and my model has a publish! method that changes a few properties on that model and potentially a few others too. Now, the Rails way suggests that I expose my model over ...

简讯

我是否可以使用某些软件来建立简便的航天国家服务器,最好是在 Java? 所有我都希望我的航天国家服务机在任何要求中都用同样的IP地址来回答。

About paypal express checkout api

In this picture,there are 3 main steps:SetExpressCheckout,GetExpressCheckoutDetails and DoExpressCheckoutDetails,I m now sure SetExpressCheckout is to be called by myself,what about ...

热门标签