Migrate to Dart 3

This commit is contained in:
Ankit Mahato
2023-08-27 20:23:41 +05:30
parent 8bb9cbcc91
commit cdba6476b0
13 changed files with 178 additions and 164 deletions

View File

@ -13,7 +13,7 @@ void main() async {
await setupInitialWindow(); await setupInitialWindow();
} else { } else {
var win = getInitialSize(); var win = getInitialSize();
await setupWindow(sz: win.$0, off: win.$1); await setupWindow(sz: win.$1, off: win.$2);
} }
runApp( runApp(
ProviderScope( ProviderScope(

View File

@ -132,17 +132,17 @@ class CollectionStateNotifier extends StateNotifier<List<RequestModel>?> {
var responseRec = var responseRec =
await request(requestModel, defaultUriScheme: defaultUriScheme); await request(requestModel, defaultUriScheme: defaultUriScheme);
late final RequestModel newRequestModel; late final RequestModel newRequestModel;
if (responseRec.$0 == null) { if (responseRec.$1 == null) {
newRequestModel = requestModel.copyWith( newRequestModel = requestModel.copyWith(
responseStatus: -1, responseStatus: -1,
message: responseRec.$2, message: responseRec.$3,
); );
} else { } else {
final responseModel = baseResponseModel.fromResponse( final responseModel = baseResponseModel.fromResponse(
response: responseRec.$0!, response: responseRec.$1!,
time: responseRec.$1!, time: responseRec.$2!,
); );
int statusCode = responseRec.$0!.statusCode; int statusCode = responseRec.$1!.statusCode;
newRequestModel = requestModel.copyWith( newRequestModel = requestModel.copyWith(
responseStatus: statusCode, responseStatus: statusCode,
message: kResponseCodeReasons[statusCode], message: kResponseCodeReasons[statusCode],

View File

@ -25,11 +25,11 @@ class _EditRequestBodyState extends ConsumerState<EditRequestBody> {
margin: kPt5o10, margin: kPt5o10,
child: Column( child: Column(
children: [ children: [
SizedBox( const SizedBox(
height: kHeaderHeight, height: kHeaderHeight,
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: const [ children: [
Text( Text(
"Select Content Type:", "Select Content Type:",
), ),

View File

@ -28,8 +28,8 @@ class _RequestEditorPaneState extends ConsumerState<RequestEditorPane> {
} else { } else {
return Padding( return Padding(
padding: kIsMacOS ? kPt24o8 : kP8, padding: kIsMacOS ? kPt24o8 : kP8,
child: Column( child: const Column(
children: const [ children: [
EditorPaneRequestURLCard(), EditorPaneRequestURLCard(),
kVSpacer10, kVSpacer10,
Expanded( Expanded(

View File

@ -28,13 +28,13 @@ class _EditorPaneRequestURLCardState extends State<EditorPaneRequestURLCard> {
), ),
borderRadius: kBorderRadius12, borderRadius: kBorderRadius12,
), ),
child: Padding( child: const Padding(
padding: const EdgeInsets.symmetric( padding: EdgeInsets.symmetric(
vertical: 5, vertical: 5,
horizontal: 20, horizontal: 20,
), ),
child: Row( child: Row(
children: const [ children: [
DropdownButtonHTTPMethod(), DropdownButtonHTTPMethod(),
kHSpacer20, kHSpacer20,
Expanded( Expanded(

View File

@ -13,8 +13,8 @@ class HomePage extends StatefulWidget {
class HomePageState extends State<HomePage> { class HomePageState extends State<HomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return const Column(
children: const [ children: [
Expanded( Expanded(
child: DashboardSplitView( child: DashboardSplitView(
sidebarWidget: CollectionPane(), sidebarWidget: CollectionPane(),

View File

@ -7,66 +7,59 @@ import 'package:apidash/models/models.dart';
import 'package:apidash/consts.dart'; import 'package:apidash/consts.dart';
Future<(http.Response?, Duration?, String?)> request( Future<(http.Response?, Duration?, String?)> request(
RequestModel requestModel, RequestModel requestModel, {
{String defaultUriScheme = kDefaultUriScheme} String defaultUriScheme = kDefaultUriScheme,
) async { }) async {
(Uri?, String?) uriRec = getValidRequestUri(requestModel.url, (Uri?, String?) uriRec = getValidRequestUri(
requestModel.url,
requestModel.requestParams, requestModel.requestParams,
defaultUriScheme: defaultUriScheme); defaultUriScheme: defaultUriScheme,
if(uriRec.$0 != null){ );
Uri requestUrl = uriRec.$0!; if (uriRec.$1 != null) {
Uri requestUrl = uriRec.$1!;
Map<String, String> headers = rowsToMap(requestModel.requestHeaders) ?? {}; Map<String, String> headers = rowsToMap(requestModel.requestHeaders) ?? {};
http.Response response; http.Response response;
String? body; String? body;
try { try {
var requestBody = requestModel.requestBody; var requestBody = requestModel.requestBody;
if(kMethodsWithBody.contains(requestModel.method) && requestBody != null){ if (kMethodsWithBody.contains(requestModel.method) &&
requestBody != null) {
var contentLength = utf8.encode(requestBody).length; var contentLength = utf8.encode(requestBody).length;
if (contentLength > 0) { if (contentLength > 0) {
body = requestBody; body = requestBody;
headers[HttpHeaders.contentLengthHeader] = contentLength.toString(); headers[HttpHeaders.contentLengthHeader] = contentLength.toString();
headers[HttpHeaders.contentTypeHeader] = kContentTypeMap[requestModel.requestBodyContentType] ?? ""; headers[HttpHeaders.contentTypeHeader] =
kContentTypeMap[requestModel.requestBodyContentType] ?? "";
} }
} }
Stopwatch stopwatch = Stopwatch()..start(); Stopwatch stopwatch = Stopwatch()..start();
switch (requestModel.method) { switch (requestModel.method) {
case HTTPVerb.get: case HTTPVerb.get:
response = await http.get(requestUrl, response = await http.get(requestUrl, headers: headers);
headers: headers);
break; break;
case HTTPVerb.head: case HTTPVerb.head:
response = await http.head(requestUrl, response = await http.head(requestUrl, headers: headers);
headers: headers);
break; break;
case HTTPVerb.post: case HTTPVerb.post:
response = await http.post(requestUrl, response = await http.post(requestUrl, headers: headers, body: body);
headers: headers,
body: body);
break; break;
case HTTPVerb.put: case HTTPVerb.put:
response = await http.put(requestUrl, response = await http.put(requestUrl, headers: headers, body: body);
headers: headers,
body: body);
break; break;
case HTTPVerb.patch: case HTTPVerb.patch:
response = await http.patch(requestUrl, response = await http.patch(requestUrl, headers: headers, body: body);
headers: headers,
body: body);
break; break;
case HTTPVerb.delete: case HTTPVerb.delete:
response = await http.delete(requestUrl, response =
headers: headers, await http.delete(requestUrl, headers: headers, body: body);
body: body);
break; break;
} }
stopwatch.stop(); stopwatch.stop();
return (response, stopwatch.elapsed, null); return (response, stopwatch.elapsed, null);
} } catch (e) {
catch (e) {
return (null, null, e.toString()); return (null, null, e.toString());
} }
} } else {
else { return (null, null, uriRec.$2);
return (null, null, uriRec.$1);
} }
} }

View File

@ -53,11 +53,8 @@ MediaType? getMediaTypeFromHeaders(Map? headers) {
return (null, false); return (null, false);
} }
(Uri?, String?) getValidRequestUri( (Uri?, String?) getValidRequestUri(String? url, List<KVRow>? requestParams,
String? url, {String defaultUriScheme = kDefaultUriScheme}) {
List<KVRow>? requestParams,
{String defaultUriScheme = kDefaultUriScheme}
) {
url = url?.trim(); url = url?.trim();
if (url == null || url == "") { if (url == null || url == "") {
return (null, "URL is missing!"); return (null, "URL is missing!");
@ -68,12 +65,11 @@ MediaType? getMediaTypeFromHeaders(Map? headers) {
} }
(String?, bool) urlScheme = getUriScheme(uri); (String?, bool) urlScheme = getUriScheme(uri);
if(urlScheme.$0 != null){ if (urlScheme.$1 != null) {
if (!urlScheme.$1){ if (!urlScheme.$2) {
return (null, "Unsupported URL Scheme (${urlScheme.$0})"); return (null, "Unsupported URL Scheme (${urlScheme.$1})");
} }
} } else {
else {
url = "$defaultUriScheme://$url"; url = "$defaultUriScheme://$url";
} }
@ -93,13 +89,17 @@ MediaType? getMediaTypeFromHeaders(Map? headers) {
return (uri, null); return (uri, null);
} }
(List<ResponseBodyView>, String?) getResponseBodyViewOptions(MediaType? mediaType){ (List<ResponseBodyView>, String?) getResponseBodyViewOptions(
MediaType? mediaType) {
if (mediaType != null) { if (mediaType != null) {
var type = mediaType.type; var type = mediaType.type;
var subtype = mediaType.subtype; var subtype = mediaType.subtype;
if (kResponseBodyViewOptions.containsKey(type)) { if (kResponseBodyViewOptions.containsKey(type)) {
if (kResponseBodyViewOptions[type]!.containsKey(subtype)) { if (kResponseBodyViewOptions[type]!.containsKey(subtype)) {
return (kResponseBodyViewOptions[type]![subtype]!, kCodeHighlighterMap[subtype] ?? subtype); return (
kResponseBodyViewOptions[type]![subtype]!,
kCodeHighlighterMap[subtype] ?? subtype
);
} }
if (subtype.contains(kSubTypeJson)) { if (subtype.contains(kSubTypeJson)) {
subtype = kSubTypeJson; subtype = kSubTypeJson;
@ -108,9 +108,15 @@ MediaType? getMediaTypeFromHeaders(Map? headers) {
subtype = kSubTypeXml; subtype = kSubTypeXml;
} }
if (kResponseBodyViewOptions[type]!.containsKey(subtype)) { if (kResponseBodyViewOptions[type]!.containsKey(subtype)) {
return (kResponseBodyViewOptions[type]![subtype]!, kCodeHighlighterMap[subtype] ?? subtype); return (
kResponseBodyViewOptions[type]![subtype]!,
kCodeHighlighterMap[subtype] ?? subtype
);
} }
return (kResponseBodyViewOptions[type]![kSubTypeDefaultViewOptions]!, subtype); return (
kResponseBodyViewOptions[type]![kSubTypeDefaultViewOptions]!,
subtype
);
} }
} }
return (kNoBodyViewOptions, null); return (kNoBodyViewOptions, null);

View File

@ -67,7 +67,12 @@ class _CodePreviewerState extends State<CodePreviewer> {
textStyle = textStyle.merge(widget.textStyle); textStyle = textStyle.merge(widget.textStyle);
} }
processed = sanitize(widget.code); processed = sanitize(widget.code);
spans = asyncGenerateSpans(processed.$0, widget.language, widget.theme, processed.$1); spans = asyncGenerateSpans(
processed.$1,
widget.language,
widget.theme,
processed.$2,
);
} }
@override @override
@ -131,12 +136,14 @@ class _CodePreviewerState extends State<CodePreviewer> {
} }
} }
Future<List<TextSpan>> asyncGenerateSpans( Future<List<TextSpan>> asyncGenerateSpans(String code, String? language,
String code, String? language, Map<String, TextStyle> theme, bool limitedLines) async { Map<String, TextStyle> theme, bool limitedLines) async {
var parsed = highlight.parse(code, language: language); var parsed = highlight.parse(code, language: language);
var spans = convert(parsed.nodes!, theme); var spans = convert(parsed.nodes!, theme);
if (limitedLines) { if (limitedLines) {
spans.add(const TextSpan(text: "\n... more.\nPreview ends here ($kCodePreviewLinesLimit lines).\nYou can check Raw for full result.")); spans.add(const TextSpan(
text:
"\n... more.\nPreview ends here ($kCodePreviewLinesLimit lines).\nYou can check Raw for full result."));
} }
return spans; return spans;
} }

View File

@ -339,8 +339,8 @@ class _ResponseBodyState extends State<ResponseBody> {
} }
var responseBodyView = getResponseBodyViewOptions(mediaType); var responseBodyView = getResponseBodyViewOptions(mediaType);
var options = responseBodyView.$0; var options = responseBodyView.$1;
var highlightLanguage = responseBodyView.$1; var highlightLanguage = responseBodyView.$2;
if (formattedBody == null) { if (formattedBody == null) {
options = [...options]; options = [...options];

View File

@ -164,13 +164,11 @@ void main() {
expect(getUriScheme(uri2), (uriScheme2Expected, false)); expect(getUriScheme(uri2), (uriScheme2Expected, false));
}); });
test('Testing getUriScheme for empty scheme value', () { test('Testing getUriScheme for empty scheme value', () {
Uri uri3 = Uri( Uri uri3 = Uri(scheme: '');
scheme: '');
expect(getUriScheme(uri3), (null, false)); expect(getUriScheme(uri3), (null, false));
}); });
test('Testing getUriScheme for null scheme value', () { test('Testing getUriScheme for null scheme value', () {
Uri uri4 = Uri( Uri uri4 = Uri(scheme: null);
scheme: null);
expect(getUriScheme(uri4), (null, false)); expect(getUriScheme(uri4), (null, false));
}); });
}); });
@ -214,9 +212,11 @@ void main() {
}); });
test('Testing getValidRequestUri when uri scheme is not supported', () { test('Testing getValidRequestUri when uri scheme is not supported', () {
String url5 = "mailto:someone@example.com"; String url5 = "mailto:someone@example.com";
expect(getValidRequestUri(url5, null), (null, "Unsupported URL Scheme (mailto)")); expect(getValidRequestUri(url5, null),
(null, "Unsupported URL Scheme (mailto)"));
}); });
test('Testing getValidRequestUri when query params in both url and kvrow', () { test('Testing getValidRequestUri when query params in both url and kvrow',
() {
String url6 = "api.foss42.com/country/data?code=IND"; String url6 = "api.foss42.com/country/data?code=IND";
KVRow kvRow6 = const KVRow("code", "US"); KVRow kvRow6 = const KVRow("code", "US");
Uri uri6Expected = Uri( Uri uri6Expected = Uri(
@ -241,62 +241,68 @@ void main() {
test('Testing getResponseBodyViewOptions for application/json', () { test('Testing getResponseBodyViewOptions for application/json', () {
MediaType mediaType1 = MediaType("application", "json"); MediaType mediaType1 = MediaType("application", "json");
var result1 = getResponseBodyViewOptions(mediaType1); var result1 = getResponseBodyViewOptions(mediaType1);
expect(result1.$0,kCodeRawBodyViewOptions); expect(result1.$1, kCodeRawBodyViewOptions);
expect(result1.$1, "json"); expect(result1.$2, "json");
}); });
test('Testing getResponseBodyViewOptions for application/xml', () { test('Testing getResponseBodyViewOptions for application/xml', () {
MediaType mediaType2 = MediaType("application", "xml"); MediaType mediaType2 = MediaType("application", "xml");
var result2 = getResponseBodyViewOptions(mediaType2); var result2 = getResponseBodyViewOptions(mediaType2);
expect(result2.$0, kCodeRawBodyViewOptions); expect(result2.$1, kCodeRawBodyViewOptions);
expect(result2.$1,"xml"); expect(result2.$2, "xml");
}); });
test('Testing getResponseBodyViewOptions for message/news a format currently not supported', () { test(
'Testing getResponseBodyViewOptions for message/news a format currently not supported',
() {
MediaType mediaType3 = MediaType("message", "news"); MediaType mediaType3 = MediaType("message", "news");
var result3 = getResponseBodyViewOptions(mediaType3); var result3 = getResponseBodyViewOptions(mediaType3);
expect(result3.$0,kNoBodyViewOptions); expect(result3.$1, kNoBodyViewOptions);
expect(result3.$1,null); expect(result3.$2, null);
}); });
test('Testing getResponseBodyViewOptions for application/calendar+json', () { test('Testing getResponseBodyViewOptions for application/calendar+json',
() {
MediaType mediaType4 = MediaType("application", "calendar+json"); MediaType mediaType4 = MediaType("application", "calendar+json");
var result4 = getResponseBodyViewOptions(mediaType4); var result4 = getResponseBodyViewOptions(mediaType4);
expect(result4.$0,kCodeRawBodyViewOptions); expect(result4.$1, kCodeRawBodyViewOptions);
expect(result4.$1, "json"); expect(result4.$2, "json");
}); });
test('Testing getResponseBodyViewOptions for image/svg+xml', () { test('Testing getResponseBodyViewOptions for image/svg+xml', () {
MediaType mediaType5 = MediaType("image", "svg+xml"); MediaType mediaType5 = MediaType("image", "svg+xml");
var result5 = getResponseBodyViewOptions(mediaType5); var result5 = getResponseBodyViewOptions(mediaType5);
expect(result5.$0,kCodeRawBodyViewOptions); expect(result5.$1, kCodeRawBodyViewOptions);
expect(result5.$1, "xml"); expect(result5.$2, "xml");
}); });
test('Testing getResponseBodyViewOptions for application/xhtml+xml', () { test('Testing getResponseBodyViewOptions for application/xhtml+xml', () {
MediaType mediaType6 = MediaType("application", "xhtml+xml"); MediaType mediaType6 = MediaType("application", "xhtml+xml");
var result6 = getResponseBodyViewOptions(mediaType6); var result6 = getResponseBodyViewOptions(mediaType6);
expect(result6.$0,kCodeRawBodyViewOptions); expect(result6.$1, kCodeRawBodyViewOptions);
expect(result6.$1, "xml"); expect(result6.$2, "xml");
}); });
test('Testing getResponseBodyViewOptions for application/xml-external-parsed-entity', () { test(
MediaType mediaType7 = MediaType("application", "xml-external-parsed-entity"); 'Testing getResponseBodyViewOptions for application/xml-external-parsed-entity',
() {
MediaType mediaType7 =
MediaType("application", "xml-external-parsed-entity");
var result7 = getResponseBodyViewOptions(mediaType7); var result7 = getResponseBodyViewOptions(mediaType7);
expect(result7.$0,kCodeRawBodyViewOptions); expect(result7.$1, kCodeRawBodyViewOptions);
expect(result7.$1, "xml"); expect(result7.$2, "xml");
}); });
test('Testing getResponseBodyViewOptions for text/html', () { test('Testing getResponseBodyViewOptions for text/html', () {
MediaType mediaType8 = MediaType("text", "html"); MediaType mediaType8 = MediaType("text", "html");
var result8 = getResponseBodyViewOptions(mediaType8); var result8 = getResponseBodyViewOptions(mediaType8);
expect(result8.$0,kCodeRawBodyViewOptions); expect(result8.$1, kCodeRawBodyViewOptions);
expect(result8.$1, "xml"); expect(result8.$2, "xml");
}); });
test('Testing getResponseBodyViewOptions for application/pdf', () { test('Testing getResponseBodyViewOptions for application/pdf', () {
MediaType mediaType9 = MediaType("application", "pdf"); MediaType mediaType9 = MediaType("application", "pdf");
var result9 = getResponseBodyViewOptions(mediaType9); var result9 = getResponseBodyViewOptions(mediaType9);
expect(result9.$0,kNoBodyViewOptions); expect(result9.$1, kNoBodyViewOptions);
expect(result9.$1, "pdf"); expect(result9.$2, "pdf");
}); });
test('Testing getResponseBodyViewOptions for text/calendar', () { test('Testing getResponseBodyViewOptions for text/calendar', () {
MediaType mediaType10 = MediaType("text", "calendar"); MediaType mediaType10 = MediaType("text", "calendar");
var result10 = getResponseBodyViewOptions(mediaType10); var result10 = getResponseBodyViewOptions(mediaType10);
expect(result10.$0,kRawBodyViewOptions); expect(result10.$1, kRawBodyViewOptions);
expect(result10.$1, "calendar"); expect(result10.$2, "calendar");
}); });
}); });
@ -365,7 +371,9 @@ void main() {
String body5 = '''<html>${getRandomStringLines(100, 10000)}</html>'''; String body5 = '''<html>${getRandomStringLines(100, 10000)}</html>''';
expect(formatBody(body5, mediaTypeHtml), null); expect(formatBody(body5, mediaTypeHtml), null);
}); });
test('Testing formatBody for html subtype values with random values within limit', () { test(
'Testing formatBody for html subtype values with random values within limit',
() {
String body6 = '''<html>${getRandomStringLines(100, 190)}</html>'''; String body6 = '''<html>${getRandomStringLines(100, 190)}</html>''';
expect(formatBody(body6, mediaTypeHtml), body6); expect(formatBody(body6, mediaTypeHtml), body6);
}); });

View File

@ -6,12 +6,12 @@ import 'package:multi_split_view/multi_split_view.dart';
void main() { void main() {
testWidgets('Testing for Dashboard Splitview', (tester) async { testWidgets('Testing for Dashboard Splitview', (tester) async {
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( const MaterialApp(
title: 'Dashboard Splitview', title: 'Dashboard Splitview',
home: Scaffold( home: Scaffold(
body: DashboardSplitView( body: DashboardSplitView(
sidebarWidget: Column(children: const [Text("Hello")]), sidebarWidget: Column(children: [Text("Hello")]),
mainWidget: Column(children: const [Text("World")]), mainWidget: Column(children: [Text("World")]),
), ),
), ),
), ),
@ -23,12 +23,12 @@ void main() {
}); });
testWidgets('Testing for Equal SplitView', (tester) async { testWidgets('Testing for Equal SplitView', (tester) async {
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( const MaterialApp(
title: 'Equal SplitView', title: 'Equal SplitView',
home: Scaffold( home: Scaffold(
body: EqualSplitView( body: EqualSplitView(
leftWidget: Column(children: const [Text("Hello equal")]), leftWidget: Column(children: [Text("Hello equal")]),
rightWidget: Column(children: const [Text("World equal")]), rightWidget: Column(children: [Text("World equal")]),
), ),
), ),
), ),

View File

@ -10,8 +10,8 @@ void main() {
MaterialApp( MaterialApp(
title: 'URL Field', title: 'URL Field',
theme: kThemeDataDark, theme: kThemeDataDark,
home: Scaffold( home: const Scaffold(
body: Column(children: const [URLField(activeId: '2')]), body: Column(children: [URLField(activeId: '2')]),
), ),
), ),
); );
@ -30,9 +30,9 @@ void main() {
MaterialApp( MaterialApp(
title: 'CellField', title: 'CellField',
theme: kThemeDataLight, theme: kThemeDataLight,
home: Scaffold( home: const Scaffold(
body: Column( body: Column(
children: const [ children: [
CellField( CellField(
keyId: "4", keyId: "4",
hintText: "Passing some hint text", hintText: "Passing some hint text",