- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如您所知,有一个无聊的动荡节目,他们制作了一个动荡的黑客新闻应用程序。如果我要实现相同的步骤,以了解应用程序的测试。我遇到了这个问题。
类型'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']
可能返回int。
.toString()
:
title: json['score'].toString() //now it is a String
关于json - 黑客新闻中的flutter小部件测试中, 'int'类型不是 'String'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63825064/
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
有时,当我看到一个我喜欢的网站或来自受人尊敬的人的网站时,我会查看源代码并尝试理解它们(就像我们所有人一样)。 关于 Jeremy Keiths他使用以下代码的网站: [role="navigatio
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
This question already has answers here: Prevent users from modifying part of the text in SLComposeVi
我正在使用 rockettheme 模板并使用自定义 css 文件编辑了一些 css 代码。 我已经设法让它在 Firefox 和 Chrome 上看起来像我想要的那样,但是 IE 看起来很奇怪。
我在 Dreamweaver 中右击我的行号,试图为一行添加书签。 我注意到这个菜单项: 什么是 Caio Hack? alert('hi'); 突出显示,结果如下: 最佳答案 这是一个古老的 CSS
我在看this SSL讲座。 Zeldovich 教授在这里说,如果连接是 http,黑客可能会让您相信您正在与 Amazon.com 对话,而实际上您正在与他的(黑客)服务器对话。 这是怎么发生的?
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我有一个相当复杂的 JTable 子类(WidgetTable 及其 WidgetTableModel),当我将它添加到虚拟 JPanel 以进行测试时它工作正常。 因为我绝对讨厌使用 LayoutM
我遇到了这个很酷的 Swift 技术的例子,但没有解释它是如何工作的或者它做了什么,尽管我已经写了相当多的 Swift 代码,但它充满了错综复杂的 Swiftisms,它目前正在扭曲我的转过头去。 如
服务器在互联网运转中为使用者提供计算或应用服务,企业的业务流程基本上都是在服务器上进行,大批量的数据信息都会存储在此,若是服务器在黑客的入侵下中毒,后果将不堪设想。想要避免黑客的入侵,我们可以从下面
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
我有一个 DropDownList,我正在尝试阻止它被用作攻击媒介。我是否可以假设用户无法实际更改 DDL 的值并回发到服务器?目前,如果我尝试在提交后更改数据包,则会抛出此 ASP.NET 错误消息
我有一堆 C++ 结构。我想将它保存到文件并再次加载它们。问题是我的一些结构是指向基类(/结构)的指针。所以我需要一种方法来找出类型并创建它。它们实际上只是 POD,它们都有公共(public)成员,
我正在使用 Sprite 作为输入按钮。它在 Firefox 和 > IE8 中运行良好。但是,在 IE7 和 8 中,ID 值显示在 png 图形上。它看起来像这样: (我会嵌入,但不允许):Pre
以下安全吗? struct K { ... } struct A { A(int psize) : size(psize), foo(nullptr), bar(nullptr) {
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我正在破解一个画廊插件,我想在其中禁用缩略图的点击事件并将其替换为悬停事件。 这就是我所做的:http://jsbin.com/enezol/3 $(function() { var galler
昨天这里有一篇帖子因问题过于本地化而被关闭。好吧,我现在遇到了同样的问题,而且这似乎是一个相当新的问题。 基本上,从昨天开始,我的防病毒软件就阻止了我的网站。该网站的 https://版本加载绝对正常
我有一个 Unity WebGL 应用程序在 Web 浏览器中运行,它使用普通的旧 Unity WWW 和 http://.... 与远程服务器通信,它调用服务器上的几个不同的 php 脚本并使用 P
我是一名优秀的程序员,十分优秀!