在黑客新闻的flutter小部件测试中,类型'int'不是类型'String'的子类型

如何解决在黑客新闻的flutter小部件测试中,类型'int'不是类型'String'的子类型

正如您已经知道的那样,他们制作了一个骇客的黑客新闻应用程序,这是一个无聊的冒牌节目。如果我要实施相同的步骤,以了解应用程序的测试。我遇到了这个问题。 类型'int'不是类型'String'的子类型。 我只实施了三个测试。 json_test.dart

import 'package:flutter_test/flutter_test.dart';
import '../lib/jsonparsing.dart';

void main() async {
  test("parses topstories.json",() {
    const jsonString =
        "[24429012,24426500,24426882,24422593,24426407,24427978,24420177,24428206,24426617,24427417,24428755,24422897,24410641,24424910,24422432,24423463,24424462,24428693,24421907,24427617,24426917,24423637,24426641,24425843,24411110,24404595,24428907,24424073,24409543,24422491,24425424,24403827,24422472,24428810,24423544,24423023,24421331,24428943,24422808,24420120,24427311,24427596,24402763,24425870,24426207,24422441,24419441,24423610,24424698,24427706,24428633,24424992,24426131,24426441,24422416,24413394,24421018,24422547,24419042,24423208,24421247,24427930,24428402,24420874,24417015,24408509,24412970,24419081,24407432,24401999,24422092,24406193,24424040,24417319,24416908,24413705,24416807,24402839,24422333,24427247,24419803,24412881,24422720,24415760,24428372,24414497,24402429,24425776,24419660,24426023,24419947,24425121,24428572,24424335,24403076,24428554,24417482,24402202,24401927,24416005,24419844,24425625,24410596,24404189,24414949,24417079,24423614,24423600,24414252,24425268,24418854,24425111,24411344,24407706,24424989,24422729,24423540,24410530,24412179,24410627,24415872,24421934,24407204,24423602,24423534,24419875,24401439,24427083,24406714,24424208,24418838,24426107,24401664,24411357,24424829,24420304,24404814,24419574,24410113,24416112,24427169,24427165,24422599,24422365,24422358,24404002,24412810,24411991,24421073,24427696,24422148,24406611,24401085,24411076,24421034,24421767,24424621,24427045,24425909,24404808,24409093,24412687,24425797,24423484,24407241,24424872,24414877,24413629,24408077,24409334,24404628,24407323,24411540,24425146,24426014,24420692,24403070,24408185,24421020,24408511,24407674,24405632,24426573,24401805,24408324,24406366,24403464,24402419,24411786,24405758,24407005,24426178,24424804,24426132,24426108,24426631,24407849,24407272,24402937,24413746,24403211,24401748,24410681,24405981,24401546,24404742,24411315,24405884,24407232,24421061,24405941,24403351,24423612,24420975,24405776,24425056,24418802,24407601,24411162,24426728,24407321,24410472,24407844,24422487,24402107,24401462,24423332,24401565,24418930,24415294,24415252,24403236,24402467,24423399,24416458,24401246,24403392,24401206,24404555,24414671,24406921,24406829,24408717,24422463,24412936,24404681,24408201,24412549,24418171,24405214,24425068,24411693,24411554,24403566,24419664,24416977,24407122,24409695,24404113,24417890,24425794,24418248,24420592,24408813,24403461,24420359,24415180,24407444,24410575,24403095,24401112,24402543,24409919,24411253,24404671,24416894,24402125,24404357,24406683,24413393,24413737,24406046,24409972,24405876,24413045,24405946,24413554,24405517,24418345,24418856,24408773,24405960,24401221,24410652,24418598,24409327,24418523,24404968,24410991,24411834,24401124,24414153,24413823,24406276,24424567,24423129,24425498,24415528,24422788,24403306,24403255,24414448,24405833,24414301,24401287,24411683,24406577,24403862,24406418,24416379,24421236,24412657,24404704,24420996,24420777,24406341,24405512,24402885,24405253,24404200,24414222,24421155,24408228,24419521,24407430,24411758,24405455,24421761,24405294,24401352,24411207,24403633,24411030,24406818,24402075,24405279,24418561,24401245,24405942,24420567,24405634,24405583,24413574,24404921,24401450,24405651,24410121,24401519,24403647,24406270,24413835,24412451,24405814,24405736,24402536,24405600,24422368,24405438,24425763,24404549,24404182,24410125,24413607,24408109,24406632,24410299,24401107,24405100,24402849,24406446,24404657,24426389,24425252,24409970,24402841,24402617,24401415,24405318,24402404,24403967,24415907,24407797,24412792,24428579,24424763]";
    expect(
      parseTopStories(jsonString).first,24429012,);
  });
  test("parses Articles.json",() {
    const jsonString =
        """{"by":"dhouston","descendants":71,"id":8863,"kids":[9224,8917,8952,8958,8884,8887,8869,8940,8908,9005,8873,9671,9067,9055,8865,8881,8872,8955,10403,8903,8928,9125,8998,8901,8902,8907,8894,8870,8878,8980,8934,8943,8876],"score":104,"time":1175714200,"title":"My YC app: Dropbox - Throw away your USB drive","type":"story","url":"http://www.getdropbox.com/u/2/screencast.html"}""";
    expect(
      parseArticle(jsonString).by,"dhouston",);
  });
}

json_parsing.dart

import './models/article.dart';
import 'dart:convert' as json;

List<int> parseTopStories(String jsonStr) {
  final parsed = json.jsonDecode(jsonStr);
  final listofIds = List<int>.from(parsed);
  return listofIds;
}

Article parseArticle(String jsonStr) {
  final parsed = json.jsonDecode(jsonStr);
  Article article = Article.fromJson(parsed);
  return article;
}

article.dart

class Article {
  final int id;
  final String title;
  final String url;
  final String type;
  final String by;
  final int time;
  final int score;
  final int descendants;
  final List<int> kids;

  const Article({
    this.id,this.title,this.url,this.type,this.by,this.time,this.score,this.descendants,this.kids,});
  factory Article.fromJson(Map<String,dynamic> json) {
    if (json == null) return null;
    return Article(
      url: json['url'] ?? null,by: json['by'],id: json['id'],time: json['time'],title: json['score'],descendants: json['descendants'],kids: json['kids'],score: json['score'],type: json['type'],);
  }
}

widget_test.dart

// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test,use the WidgetTester
// utility that Flutter provides. For example,you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree,read text,and verify that the values of widget properties are correct.

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

import 'package:hacker_app/main.dart';

void main() {
  testWidgets('Hacker News',(WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(MyApp());
    expect(find.byIcon(Icons.launch),findsNothing);

    await tester.tap(find.byType(ExpansionTile).first);

    await tester.pumpAndSettle();
    expect(find.byIcon(Icons.launch),findsOneWidget);
  });
}

00:06 +1 -1: C:\Users\Shary\Desktop\Development\practice\hackerapp\test\json_test.dart: parses item.json [E]
  type 'int' is not a subtype of type 'String'
  ..\lib\models\article.dart 30:18  new Article.fromJson
  ..\lib\jsonparsing.dart 12:29     parseArticle
  json_test.dart 17:7               main.<fn>

解决方法

编辑:尽管我的回答回答了您提出的问题,如另一个回答中所指出,您可能不想使用title: json['score'],而是使用title: json['title']

您正在尝试将int用作字符串。查看错误代码,它将告诉您导致错误的行。

根据您的错误代码,该错误发生在article.dart的第30行。

这个:

title: json['score']

title可能期望String,而json['score']可能返回整数。

只需添加.toString(),如下所示:

title: json['score'].toString()  //now it is a String
,

score属性的JSON分析错误,您必须使用带有全局提及的String的相应字段

替换

 factory Article.fromJson(Map<String,dynamic> json) {
    if (json == null) return null;
    return Article(
      url: json['url'] ?? null,by: json['by'],id: json['id'],time: json['time'],title: json['score'],// This should be title as score you alredy added at the bottom
      descendants: json['descendants'],kids: json['kids'],score: json['score'],type: json['type'],);
  }

使用

 factory Article.fromJson(Map<String,title: json['title'],descendants: json['descendants'],);
  }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-