Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null DataList issue + other null-safety concerns #86

Open
wants to merge 9 commits into
base: null-safety
Choose a base branch
from
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: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ res/
android/

lib/selectable_tags_back.dart
lib/generated_plugin_registrant.dart

example/lib/main_back.dart
example/android/local.properties
example/android/app/src/main/java/*
example/ios/Flutter/*
2 changes: 2 additions & 0 deletions example/ios/Flutter/flutter_export_environment.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export "FLUTTER_APPLICATION_PATH=D:\Android\Flutter\package\flutter_tags\example
export "FLUTTER_TARGET=lib\main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build\ios"
export "OTHER_LDFLAGS=$(inherited) -framework Flutter"
export "FLUTTER_FRAMEWORK_DIR=C:\flutter\bin\cache\artifacts\engine\ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
export "DART_OBFUSCATION=false"
Expand Down
2 changes: 0 additions & 2 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import 'package:flutter/material.dart';

void main() {}
17 changes: 9 additions & 8 deletions lib/src/item_tags.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,21 @@ class _ItemTagsState extends State<ItemTags> {
_dataListInherited = DataListInherited.of(context);

// set List length
if (_dataListInherited!.list!.length < _dataListInherited!.itemCount!)
if (_dataListInherited!.itemCount != null &&
_dataListInherited!.list!.length < _dataListInherited!.itemCount!) {
_dataListInherited!.list!.length = _dataListInherited!.itemCount!;
}

if (_dataListInherited!.list!.length > (widget.index + 1) &&
_dataListInherited!.list!.elementAt(widget.index) != null &&
_dataListInherited!.list!.elementAt(widget.index).title !=
_dataListInherited!.list!.elementAt(widget.index)!.title !=
widget.title) {
// when an element is removed from the data source
_dataListInherited!.list!.removeAt(widget.index);

// when all item list changed in data source
if (_dataListInherited!.list!.elementAt(widget.index) != null &&
_dataListInherited!.list!.elementAt(widget.index).title !=
_dataListInherited!.list!.elementAt(widget.index)!.title !=
widget.title)
_dataListInherited!.list!
.removeRange(widget.index, _dataListInherited!.list!.length);
Expand Down Expand Up @@ -415,16 +417,15 @@ class _ItemTagsState extends State<ItemTags> {
case MainAxisAlignment.spaceBetween:
case MainAxisAlignment.start:
return TextAlign.start;
break;

case MainAxisAlignment.end:
return TextAlign.end;
break;

case MainAxisAlignment.spaceAround:
case MainAxisAlignment.spaceEvenly:
case MainAxisAlignment.center:
return TextAlign.center;
}
return null;
}

///TextStyle
Expand All @@ -438,9 +439,9 @@ class _ItemTagsState extends State<ItemTags> {
void _singleItem(DataListInherited dataSetIn, DataList? dataSet) {
dataSetIn.list!
.where((tg) => tg != null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid use of !.

Suggested change
.where((tg) => tg != null)
..whereNotNull()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks but my IDE complains when using whereNotNull, as if it didn't exist...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, it is an extension method from the collection package and it is not used in this package, I'm sorry. (and sorry for the very long delay)

.where((tg) => tg.active)
.where((tg) => tg!.active)
.where((tg2) => tg2 != dataSet)
.forEach((tg) => tg.active = false);
.forEach((tg) => tg!.active = false);
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/src/suggestions_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ typedef OnSubmittedCallback = void Function(String string);
class SuggestionsTextField extends StatefulWidget {
SuggestionsTextField(
{required this.tagsTextField, this.onSubmitted, Key? key})
: assert(tagsTextField != null),
super(key: key);
: super(key: key);

final TagsTextField tagsTextField;
final OnSubmittedCallback? onSubmitted;
Expand Down
17 changes: 6 additions & 11 deletions lib/src/tags.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_tags/src/suggestions_textfield.dart';

import '../flutter_tags.dart';
import 'util/custom_wrap.dart';
import 'package:flutter_tags/src/suggestions_textfield.dart';

///ItemBuilder
typedef Widget ItemBuilder(int index);
Expand All @@ -25,11 +25,6 @@ class Tags extends StatefulWidget {
this.textField,
Key? key})
: assert(itemCount >= 0),
assert(alignment != null),
assert(runAlignment != null),
assert(direction != null),
assert(verticalDirection != null),
assert(textDirection != null),
super(key: key);

///specific number of columns
Expand Down Expand Up @@ -87,9 +82,9 @@ class TagsState extends State<Tags> {
Orientation _orientation = Orientation.portrait;
double _width = 0;

final List<DataList> _list = [];
final List<DataList?> _list = [];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the fix to #79


List<Item> get getAllItem => _list.toList();
List<Item?> get getAllItem => _list.toList();

//get the current width of the screen
void _getWidthContext() {
Expand Down Expand Up @@ -164,11 +159,11 @@ class TagsState extends State<Tags> {
tagsTextField: widget.textField!,
onSubmitted: (String str) {
if (!widget.textField!.duplicates) {
final List<DataList> lst =
final List<DataList?> lst =
_list.where((l) => l != null && l.title == str).toList();

if (lst.isNotEmpty) {
lst.forEach((d) => d.showDuplicate = true);
lst.forEach((d) => d!.showDuplicate = true);
return;
}
}
Expand Down Expand Up @@ -240,7 +235,7 @@ class DataListInherited extends InheritedWidget {
required Widget child})
: super(key: key, child: child);

final List<DataList>? list;
final List<DataList?>? list;
final bool? symmetry;
final int? itemCount;

Expand Down
34 changes: 7 additions & 27 deletions lib/src/util/custom_wrap.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:math' as math;

import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

Expand Down Expand Up @@ -104,13 +105,7 @@ class CustomRenderWrap extends RenderBox
WrapCrossAlignment crossAxisAlignment = WrapCrossAlignment.start,
TextDirection? textDirection,
VerticalDirection verticalDirection = VerticalDirection.down,
}) : assert(direction != null),
assert(alignment != null),
assert(spacing != null),
assert(runAlignment != null),
assert(runSpacing != null),
assert(crossAxisAlignment != null),
_column = column,
}) : _column = column,
_symmetry = symmetry,
_direction = direction,
_alignment = alignment,
Expand Down Expand Up @@ -142,7 +137,6 @@ class CustomRenderWrap extends RenderBox
Axis get direction => _direction;
Axis _direction;
set direction(Axis value) {
assert(value != null);
if (_direction == value) return;
_direction = value;
markNeedsLayout();
Expand All @@ -151,7 +145,6 @@ class CustomRenderWrap extends RenderBox
WrapAlignment get alignment => _alignment;
WrapAlignment _alignment;
set alignment(WrapAlignment value) {
assert(value != null);
if (_alignment == value) return;
_alignment = value;
markNeedsLayout();
Expand All @@ -160,7 +153,6 @@ class CustomRenderWrap extends RenderBox
double get spacing => _spacing;
double _spacing;
set spacing(double value) {
assert(value != null);
if (_spacing == value) return;
_spacing = value;
markNeedsLayout();
Expand All @@ -169,7 +161,6 @@ class CustomRenderWrap extends RenderBox
WrapAlignment get runAlignment => _runAlignment;
WrapAlignment _runAlignment;
set runAlignment(WrapAlignment value) {
assert(value != null);
if (_runAlignment == value) return;
_runAlignment = value;
markNeedsLayout();
Expand All @@ -178,7 +169,6 @@ class CustomRenderWrap extends RenderBox
double get runSpacing => _runSpacing;
double _runSpacing;
set runSpacing(double value) {
assert(value != null);
if (_runSpacing == value) return;
_runSpacing = value;
markNeedsLayout();
Expand All @@ -187,7 +177,6 @@ class CustomRenderWrap extends RenderBox
WrapCrossAlignment get crossAxisAlignment => _crossAxisAlignment;
WrapCrossAlignment _crossAxisAlignment;
set crossAxisAlignment(WrapCrossAlignment value) {
assert(value != null);
if (_crossAxisAlignment == value) return;
_crossAxisAlignment = value;
markNeedsLayout();
Expand All @@ -202,20 +191,17 @@ class CustomRenderWrap extends RenderBox
}
}

VerticalDirection get verticalDirection => _verticalDirection;
VerticalDirection _verticalDirection;
set verticalDirection(VerticalDirection value) {
VerticalDirection? get verticalDirection => _verticalDirection;
VerticalDirection? _verticalDirection;

set verticalDirection(VerticalDirection? value) {
if (_verticalDirection != value) {
_verticalDirection = value;
markNeedsLayout();
}
}

bool get _debugHasNecessaryDirections {
assert(direction != null);
assert(alignment != null);
assert(runAlignment != null);
assert(crossAxisAlignment != null);
if (firstChild != null && lastChild != firstChild) {
// i.e. there's more than one child
switch (direction) {
Expand Down Expand Up @@ -415,7 +401,6 @@ class CustomRenderWrap extends RenderBox
case Axis.vertical:
return child.size.height;
}
return 0.0;
}

double _getCrossAxisExtent(RenderBox child) {
Expand All @@ -425,7 +410,6 @@ class CustomRenderWrap extends RenderBox
case Axis.vertical:
return child.size.width;
}
return 0.0;
}

Offset _getOffset(double mainAxisOffset, double crossAxisOffset) {
Expand All @@ -435,7 +419,6 @@ class CustomRenderWrap extends RenderBox
case Axis.vertical:
return Offset(crossAxisOffset, mainAxisOffset);
}
return Offset.zero;
}

double _getChildCrossAxisOffset(bool flipCrossAxis, double runCrossAxisExtent,
Expand All @@ -449,7 +432,6 @@ class CustomRenderWrap extends RenderBox
case WrapCrossAlignment.center:
return freeSpace / 2.0;
}
return 0.0;
}

bool _hasVisualOverflow = false;
Expand All @@ -463,7 +445,7 @@ class CustomRenderWrap extends RenderBox
size = constraints.smallest;
return;
}
BoxConstraints? childConstraints;
BoxConstraints childConstraints;
double mainAxisLimit = 0.0;
bool flipMainAxis = false;
bool flipCrossAxis = false;
Expand All @@ -481,8 +463,6 @@ class CustomRenderWrap extends RenderBox
if (textDirection == TextDirection.rtl) flipCrossAxis = true;
break;
}
assert(childConstraints != null);
assert(mainAxisLimit != null);
final double spacing = this.spacing;
final double runSpacing = this.runSpacing;
final List<_RunMetrics> runMetrics = <_RunMetrics>[];
Expand Down
Loading