Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion example/lib/examples/github_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class JsonViewApp extends StatelessWidget {
appBar: AppBar(title: Text('flutter_json_view 1.1.2')),
body: Padding(
padding: const EdgeInsets.all(10),
child: JsonView.asset('assets/github_user.json'),
child: JsonView.asset(
'assets/github_user.json',
onError: const Text("Failed to load Asset"),
),
),
),
);
Expand Down
1 change: 1 addition & 0 deletions example/lib/examples/green_custom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class JsonViewApp extends StatelessWidget {
),
),
),
onError: const Text("Failed to parse String"),
),
),
),
Expand Down
1 change: 1 addition & 0 deletions example/lib/examples/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class JsonViewApp extends StatelessWidget {
'height': 186.5
},
},
onError: Text("Failed to parse map"),
),
),
),
Expand Down
1 change: 1 addition & 0 deletions example/lib/examples/pink.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class JsonViewApp extends StatelessWidget {
color: Colors.purple,
),
),
onError: const Text("Failed to parse String"),
),
),
),
Expand Down
3 changes: 3 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ class _HomeScreenState extends State<_HomeScreen> {
JsonView.string(
'{"author":{"name": "Stas", "lastName": "Ilin", "githubLogin": "Frezyx", "age": 19, "man": true, "height": 186.5}}',
theme: const JsonViewTheme(viewType: JsonViewType.collapsible),
onError: const Text("Failed to parse String"),
),
const SizedBox(height: 10),
Text('Base', style: theme.textTheme.headlineSmall),
const SizedBox(height: 2),
JsonView.string(
'{"author":{"name": "Stas", "lastName": "Ilin", "githubLogin": "Frezyx", "age": 19, "man": true, "height": 186.5}}',
theme: const JsonViewTheme(viewType: JsonViewType.base),
onError: const Text("Failed to parse String"),
),
const SizedBox(height: 10),
Text('Big data', style: theme.textTheme.headlineSmall),
Expand All @@ -62,6 +64,7 @@ class _HomeScreenState extends State<_HomeScreen> {
'assets/github_user.json',
theme:
const JsonViewTheme(viewType: JsonViewType.collapsible),
onError: const Text("Failed to parse JSON"),
),
),
],
Expand Down
4 changes: 3 additions & 1 deletion lib/src/builders/asset_json_view_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import 'package:flutter_json_view/src/widgets/widgets.dart';
import 'builders.dart';

class AssetJsonViewBuilder implements JsonViewBuilder {
AssetJsonViewBuilder(this.path, {JsonViewTheme? jsonViewTheme})
AssetJsonViewBuilder(this.path, this.onError, {JsonViewTheme? jsonViewTheme})
: _jsonViewTheme = jsonViewTheme ?? const JsonViewTheme();

final String path;
final dynamic onError;
final JsonViewTheme _jsonViewTheme;

@override
Widget build() {
return JsonLoaderItem(
path: path,
onError: onError,
jsonViewTheme: _jsonViewTheme,
);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/builders/common_json_view_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CommonJsonViewBuilder implements JsonViewBuilder {
: _jsonViewTheme = jsonViewTheme;

final dynamic jsonObj;

final JsonViewTheme _jsonViewTheme;

@override
Expand All @@ -31,7 +32,10 @@ class CommonJsonViewBuilder implements JsonViewBuilder {
jsonObj: jsonObj as List,
jsonViewTheme: _jsonViewTheme,
);
} else if (jsonObj is Widget) {
return jsonObj;
}
;
return PrimitiveBuilder(
jsonObj,
jsonViewTheme: _jsonViewTheme,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/builders/map_json_view_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import 'package:flutter_json_view/src/theme/json_view_theme.dart';
import 'builders.dart';

class MapJsonViewBuilder implements JsonViewBuilder {
MapJsonViewBuilder(this.map, {JsonViewTheme? jsonViewTheme})
MapJsonViewBuilder(this.map, this.onError, {JsonViewTheme? jsonViewTheme})
: _commonBuilder = CommonJsonViewBuilder(
map,
jsonViewTheme: jsonViewTheme ?? const JsonViewTheme(),
);

final Map<String, dynamic> map;
final dynamic onError;
final JsonViewBuilder _commonBuilder;

@override
Expand Down
6 changes: 4 additions & 2 deletions lib/src/builders/string_json_view_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import 'package:flutter_json_view/src/utils/utils.dart';
import 'builders.dart';

class StringJsonViewBuilder implements JsonViewBuilder {
StringJsonViewBuilder(this.jsonString, {JsonViewTheme? jsonViewTheme})
StringJsonViewBuilder(this.jsonString, this.onError,
{JsonViewTheme? jsonViewTheme})
: _commonBuilder = CommonJsonViewBuilder(
JsonConverter.jsonStringToObject(jsonString),
JsonConverter.jsonStringToObject(jsonString, onError),
jsonViewTheme: jsonViewTheme ?? const JsonViewTheme(),
);

final String jsonString;
final dynamic onError;
final JsonViewBuilder _commonBuilder;

@override
Expand Down
11 changes: 11 additions & 0 deletions lib/src/flutter_json_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ class JsonView extends StatefulWidget {
JsonView.string(
String jsonString, {
Key? key,
required Widget onError,
JsonViewTheme? theme,
}) : _stringData = jsonString,
_mapData = null,
_assetsPath = null,
_onError = onError,
_builder = StringJsonViewBuilder(
jsonString,
onError,
jsonViewTheme: theme,
),
super(key: key);
Expand All @@ -32,12 +35,15 @@ class JsonView extends StatefulWidget {
JsonView.asset(
String path, {
Key? key,
required Widget onError,
JsonViewTheme? theme,
}) : _assetsPath = path,
_mapData = null,
_stringData = null,
_onError = onError,
_builder = AssetJsonViewBuilder(
path,
onError,
jsonViewTheme: theme,
),
super(key: key);
Expand All @@ -47,19 +53,23 @@ class JsonView extends StatefulWidget {
JsonView.map(
Map<String, dynamic> map, {
Key? key,
required Widget onError,
JsonViewTheme? theme,
}) : _mapData = map,
_stringData = null,
_assetsPath = null,
_onError = onError,
_builder = MapJsonViewBuilder(
map,
onError,
jsonViewTheme: theme,
),
super(key: key);

final Map<String, dynamic>? _mapData;
final String? _stringData;
final String? _assetsPath;
final Widget _onError;
final JsonViewBuilder _builder;
static PrimitiveJsonItemBuilder? primitiveJsonItemBuilder;

Expand All @@ -82,6 +92,7 @@ class _JsonViewState extends State<JsonView> {
jsonView = BaseJsonView(
jsonData: jsonData,
assetsPath: widget._assetsPath,
onError: widget._onError,
jsonViewTheme: widget._builder.jsonViewTheme,
);
break;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/utils/asset_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ abstract class AssetLoader {
return rootBundle.loadString(path);
}

static Future<dynamic> getAssetJson(String filePath) async {
static Future<dynamic> getAssetJson(String filePath, onError) async {
final jsonString = await _loadAssets(filePath);
return JsonConverter.jsonStringToObject(jsonString);
return JsonConverter.jsonStringToObject(jsonString, onError);
}
}
9 changes: 6 additions & 3 deletions lib/src/utils/converter.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:convert';

abstract class JsonConverter {
static dynamic jsonStringToObject(String jsonString) {
assert(canConverToObject(jsonString));
return json.decode(jsonString);
static dynamic jsonStringToObject(String jsonString, dynamic onError) {
try {
return json.decode(jsonString);
} catch (_) {
return onError;
}
}

static bool canConverToObject(String jsonString) {
Expand Down
5 changes: 4 additions & 1 deletion lib/src/widgets/base_json_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ class BaseJsonView extends StatefulWidget {
Key? key,
required this.jsonViewTheme,
this.jsonData,
required this.onError,
this.assetsPath,
}) : super(key: key);

final String? jsonData;
final Widget onError;
final JsonViewTheme jsonViewTheme;
final String? assetsPath;

Expand All @@ -31,7 +33,8 @@ class _BaseJsonViewState extends State<BaseJsonView> {

Future<void> _loadAssetsJson() async {
if (widget.assetsPath != null) {
final json = await AssetLoader.getAssetJson(widget.assetsPath!);
final json =
await AssetLoader.getAssetJson(widget.assetsPath!, widget.onError);
_assetsJsonString = _encoder.convert(json);
setState(() {});
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/widgets/json_loader_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class JsonLoaderItem extends StatefulWidget {
const JsonLoaderItem({
Key? key,
required this.path,
required this.onError,
required this.jsonViewTheme,
}) : super(key: key);
final String path;
final JsonViewTheme jsonViewTheme;
final Widget onError;

@override
State<JsonLoaderItem> createState() => _JsonLoaderItemState();
Expand All @@ -31,7 +33,7 @@ class _JsonLoaderItemState extends State<JsonLoaderItem> {
}

Future<void> _initializeBuilder() async {
final json = await AssetLoader.getAssetJson(widget.path);
final json = await AssetLoader.getAssetJson(widget.path, widget.onError);
setState(() {
_commonBuilder = CommonJsonViewBuilder(
json,
Expand Down
Loading