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

Make inspector polyfill compatible with both null safe and legacy Flutter #2387

Merged
merged 1 commit into from
Sep 30, 2020

Conversation

jacob314
Copy link
Contributor

No description provided.

final String groupName = parameters['groupName'];
Map<String, Object> result = {};
final id = parameters['id'];
final subtreeDepth = int.parse(parameters['subtreeDepth'] as String);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

writing as Foo is equivalent to writing !

<String, Object>{
) as Object;
try {
final constraints = renderObject.constraints;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the constraints property is now non-nullable but could throw an exception if it is accessed at the wrong time..

@@ -29,6 +29,17 @@
// additional imports to this file.
// ignore_for_file: unused_import

// This code needs to execute both in null safe and legacy versions of Dart
// so it needs to be written in a way that is null safe without depending on any
// of the new null safety syntax. Te test that this script works with null safe
Copy link
Member

Choose a reason for hiding this comment

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

typo: to

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

final id = parameters['id'];
final subtreeDepth = int.parse(parameters['subtreeDepth'] as String);
final groupName = parameters['groupName'];
var result = <String, dynamic>{};
Copy link
Member

Choose a reason for hiding this comment

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

why the change from Object to dynamic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this has to be dynamic because that is now the type on the Flutter side. Before null safety, Object and dynamic basically meant the same thing so you could use them interchangeably in generics without getting warnings. With null safety they are different because
Object? and Object are different.

final Object value = node.value as Object;
if (value is Element) {
final renderObject = value.renderObject;
if (renderObject != null) {
Copy link
Member

Choose a reason for hiding this comment

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

should this be checking that renderObject.toDiagnosticsNode != null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a typo in your question? renderObject.toDiagnosticsNode is a Function.
Do you mean checking that the result of toDiagnosticsNode() is not null? It is now enforced as null safe in Flutter and has always been null safe by convention. The previous check was overly paranoid and triggered a lint error with null safety.

return additionalJson;
}),
) as Map<String, dynamic>;
return Future.value(<String, dynamic>{
Copy link
Member

Choose a reason for hiding this comment

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

again, why the change from Object to dynamic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

same answer as above.

@jacob314
Copy link
Contributor Author

Reverted the pubspec.lock update to avoid a deprecated method issue with the latest vm_service package version.

@jacob314 jacob314 requested a review from terrylucas September 30, 2020 00:34
@@ -70,8 +81,8 @@ import 'package:flutter/src/widgets/widget_inspector.dart';
// WidgetInspectorService show up as in scope as far as the analysis server is
// concerned.
extension WidgetInspectorServicePrivateMethods on WidgetInspectorService {
Map<String, Object> _nodeToJson(
DiagnosticsNode node,
Map<String, Object /*?*/ > /*?*/ _nodeToJson(
Copy link
Contributor

Choose a reason for hiding this comment

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

This and below would it be easier to find the null safety code by using a TODO or NNBD type of line comment e.g.,
// NNBD (jacob):
// Map<String, Object?>?_nodeToJson(
// DiagnosticsNode? node,
// Pre-NNBD(jacob):
Map<String, Object> _nodeToJson(
DiagnosticsNode node,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that is a bit cleaner. I'll add that refactor in the morning.

}
if (renderObject is RenderBox) {
additionalJson['size'] = <String, Object>{
'width': renderObject.size.width.toString(),
'height': renderObject.size.height.toString(),
};

final ParentData parentData = renderObject.parentData;
final ParentData parentData =
renderObject.parentData as ParentData;
Copy link
Contributor

Choose a reason for hiding this comment

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

final ParentData and as ParentData don't these do the same? Only need one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are right. these types don't need to be on the LHS but they do need to be on the RHS. This code matches flutter style of putting types everywhere so it is easier to merge into package:flutter.

final String id = parameters['id'];
final FlexFit flexFit =
toEnumEntry<FlexFit>(FlexFit.values, parameters['flexFit']);
final String id = parameters['id'] as String;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question final String and as String only one needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

answered lower down.

final String id = parameters['id'];
final String flexFactor = parameters['flexFactor'];
final int factor = flexFactor == 'null' ? null : int.parse(flexFactor);
final String id = parameters['id'] as String;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question on these two too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

parameters['id'] might be null.
In null safe dart the ideal way to write this is
final id = parameters['id']!;
but you can't use ! and make the code compatible with legacy dart.

}
return Future.value({'result': succeed});
}

Future<Map<String, dynamic>> setFlexProperties(
Map<String, String> parameters) {
final String id = parameters['id'];
final String id = parameters['id'] as String;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question String and as String?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

parameters['id'] might be null.
In null safe dart the ideal way to write this is
final id = parameters['id']!;
but you can't use ! and make the code compatible with legacy dart.

@jacob314 jacob314 merged commit d418d40 into flutter:master Sep 30, 2020
@jacob314 jacob314 deleted the null_safe branch September 30, 2020 05:14
@kenzieschmoll
Copy link
Member

lgtm_thumbs_up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants