fix: null-safety issues with widgets

Signed-off-by: TheOneWithTheBraid <the-one@with-the-braid.cf>
This commit is contained in:
TheOneWithTheBraid 2022-02-02 16:17:22 +01:00
parent fa6bd12294
commit 7d2a99b500
1 changed files with 21 additions and 17 deletions

View File

@ -2,37 +2,40 @@ import 'package:matrix/src/room.dart';
class MatrixWidget { class MatrixWidget {
final Room room; final Room room;
final String creatorUserId; final String? creatorUserId;
final Map<String, dynamic> data; final Map<String, dynamic>? data;
final String id; final String? id;
final String name; final String name;
final String type; final String type;
/// use [buildWidgetUrl] instead
final String url; final String url;
final bool waitForIframeLoad; final bool waitForIframeLoad;
MatrixWidget({ MatrixWidget({
required this.room, required this.room,
required this.creatorUserId, this.creatorUserId,
required this.data, this.data = const {},
required this.id, this.id,
required this.name, required this.name,
required this.type, required this.type,
/// use [buildWidgetUrl] instead
required this.url, required this.url,
required this.waitForIframeLoad, this.waitForIframeLoad = false,
}); });
factory MatrixWidget.fromJson(Map<String, dynamic> json, Room room) => factory MatrixWidget.fromJson(Map<String, dynamic> json, Room room) =>
MatrixWidget( MatrixWidget(
room: room, room: room,
creatorUserId: json['creatorUserId'], creatorUserId:
data: json['data'], json.containsKey('creatorUserId') ? json['creatorUserId'] : null,
id: json['id'], data: json.containsKey('data') ? json['data'] : {},
id: json.containsKey('id') ? json['id'] : null,
name: json['name'], name: json['name'],
type: json['type'], type: json['type'],
url: json['url'], url: json['url'],
waitForIframeLoad: json['waitForIframeLoad'], waitForIframeLoad: json.containsKey('waitForIframeLoad')
? json['waitForIframeLoad']
: false,
); );
Future<Uri> buildWidgetUrl() async { Future<Uri> buildWidgetUrl() async {
@ -50,10 +53,11 @@ class MatrixWidget {
r'$matrix_avatar_url': userProfile.avatarUrl?.toString() ?? '', r'$matrix_avatar_url': userProfile.avatarUrl?.toString() ?? '',
// removing potentially dangerous keys containing anything but // removing potentially dangerous keys containing anything but
// `[a-zA-Z0-9_-]` as well as non string values // `[a-zA-Z0-9_-]` as well as non string values
...Map.from(data) if (data != null)
..removeWhere((key, value) => ...Map.from(data!)
!RegExp(r'^[\w-]+$').hasMatch(key) || !value is String) ..removeWhere((key, value) =>
..map((key, value) => MapEntry('\$key', value)), !RegExp(r'^[\w-]+$').hasMatch(key) || !value is String)
..map((key, value) => MapEntry('\$key', value)),
}; };
replaceMap.forEach((key, value) { replaceMap.forEach((key, value) {