【问题标题】:flutter - Error: A value of type 'Articles' can't be assigned to a variable of type 'int'颤振 - 错误:“Articles”类型的值不能分配给“int”类型的变量
【发布时间】:2021-03-28 01:22:48
【问题描述】:

我正在尝试使用 https://conduit.productionready.io 创建中等克隆,当我调用 API https://conduit.productionready.io/api/articles 时,我将数据存储在 Article 模型中,然后使用 map.toList() 方法进行打印,但是

我收到一个错误:flutter - 错误:“Articles”类型的值无法分配给“int”类型的变量。

lib/screens/myhome_screen.dart:47:49: Error: A value of type 'Articles' can't be assigned to a variable of type 'int'.
 - 'Articles' is from 'package:conduit/models/globally_articles.dart' ('lib/models/globally_articles.dart').
                Text("${globalArticles.articles[index].title}"),

my_homescreen.dart

 import 'dart:convert';

import 'package:conduit/models/globally_articles.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class GlobalFeed extends StatefulWidget {
  @override
  _GlobalFeedState createState() => _GlobalFeedState();
}

class _GlobalFeedState extends State<GlobalFeed> {
  bool isLoading = true;

  GlobalArticles globalArticles;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getGlobalArticles();
    setState(() {
      isLoading = false;
    });
  }

  getGlobalArticles() async {
    var url = 'https://conduit.productionready.io/api/articles';
    var response = await http.get(url);

    setState(() {
      var jsonData = jsonDecode(response.body);
      globalArticles = GlobalArticles.fromJson(jsonData);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
            children: globalArticles.articles.map((index) {
              return Card(
                child: Column(
                  children: [
                    Text("${globalArticles.articles[index].title}"),
                  ],
                ),
              );
            }).toList()),
      ),
    );
  }
}

globalArticles.dart

class GlobalArticles {
  List<Articles> articles;
  int articlesCount;

  GlobalArticles({this.articles, this.articlesCount});

  GlobalArticles.fromJson(Map<String, dynamic> json) {
    if (json['articles'] != null) {
      articles = new List<Articles>();
      json['articles'].forEach((v) {
        articles.add(new Articles.fromJson(v));
      });
    }
    articlesCount = json['articlesCount'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.articles != null) {
      data['articles'] = this.articles.map((v) => v.toJson()).toList();
    }
    data['articlesCount'] = this.articlesCount;
    return data;
  }
}

class Articles {
  String title;
  String slug;
  String body;
  String createdAt;
  String updatedAt;
  List<String> tagList;
  String description;
  Author author;
  bool favorited;
  int favoritesCount;

  Articles(
      {this.title,
        this.slug,
        this.body,
        this.createdAt,
        this.updatedAt,
        this.tagList,
        this.description,
        this.author,
        this.favorited,
        this.favoritesCount});

  Articles.fromJson(Map<String, dynamic> json) {
    title = json['title'];
    slug = json['slug'];
    body = json['body'];
    createdAt = json['createdAt'];
    updatedAt = json['updatedAt'];
    tagList = json['tagList'].cast<String>();
    description = json['description'];
    author =
    json['author'] != null ? new Author.fromJson(json['author']) : null;
    favorited = json['favorited'];
    favoritesCount = json['favoritesCount'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    data['slug'] = this.slug;
    data['body'] = this.body;
    data['createdAt'] = this.createdAt;
    data['updatedAt'] = this.updatedAt;
    data['tagList'] = this.tagList;
    data['description'] = this.description;
    if (this.author != null) {
      data['author'] = this.author.toJson();
    }
    data['favorited'] = this.favorited;
    data['favoritesCount'] = this.favoritesCount;
    return data;
  }
}

class Author {
  String username;
  String bio;
  String image;
  bool following;

  Author({this.username, this.bio, this.image, this.following});

  Author.fromJson(Map<String, dynamic> json) {
    username = json['username'];
    bio = json['bio'];
    image = json['image'];
    following = json['following'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['username'] = this.username;
    data['bio'] = this.bio;
    data['image'] = this.image;
    data['following'] = this.following;
    return data;
  }
}

【问题讨论】:

    标签: json api flutter dart


    【解决方案1】:

    对不起,我的错...

    我换了

      Text("${globalArticles.articles[index].title}"),
    

       Text("${index.title}"),
    

    一切正常

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 2021-12-10
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      相关资源
      最近更新 更多