Uri.parse('').pathSegments 生成的 List<String> 中的错误 "Unsupported operation: removeLast"
error "Unsupported operation: removeLast" from List<String> generated by Uri.parse('').pathSegments
运行 dartpad 下面的代码目前 Based on Flutter 1.23.0-18.1.pre Dart SDK 2.10.4
import 'package:flutter/material.dart';
void main() {
final _list = ['1', '2', '3', '4'];
print('_list is a ${_list.runtimeType}');
print('${_list.last}');
try {
_list.removeLast();
} catch (e) {
print(e);
}
print('${_list.last}');
final _routeInfo = RouteInformation(location: '/user/info/5');
final _segments = Uri.parse(_routeInfo.location).pathSegments;
print('_segments is a ${_segments.runtimeType}');
print('${_segments.last}');
try {
_segments.removeLast();
} catch (e) {
print(e);
}
print('${_segments.last}');
}
我在下面有这个输出:
_list is a List<String>
4
3
_segments is a List<String>
5
Unsupported operation: removeLast
5
我不明白,我错过了什么?
显然按如下方式包装列表可以解决问题
final _segments = [...Uri.parse(_routeInfo.location).pathSegments];
import 'package:flutter/material.dart';
void main() {
final _list = ['1', '2', '3', '4'];
print('_list is a ${_list.runtimeType}');
print('${_list.last}');
try {
_list.removeLast();
} catch (e) {
print(e);
}
print('${_list.last}');
final _routeInfo = RouteInformation(location: '/user/info/5');
- final _segments = Uri.parse(_routeInfo.location).pathSegments;
+ final _segments = [...Uri.parse(_routeInfo.location).pathSegments];
print('_segments is a ${_segments.runtimeType}');
print('${_segments.last}');
try {
_segments.removeLast();
} catch (e) {
print(e);
}
print('${_segments.last}');
}
运行 dartpad 下面的代码目前 Based on Flutter 1.23.0-18.1.pre Dart SDK 2.10.4
import 'package:flutter/material.dart';
void main() {
final _list = ['1', '2', '3', '4'];
print('_list is a ${_list.runtimeType}');
print('${_list.last}');
try {
_list.removeLast();
} catch (e) {
print(e);
}
print('${_list.last}');
final _routeInfo = RouteInformation(location: '/user/info/5');
final _segments = Uri.parse(_routeInfo.location).pathSegments;
print('_segments is a ${_segments.runtimeType}');
print('${_segments.last}');
try {
_segments.removeLast();
} catch (e) {
print(e);
}
print('${_segments.last}');
}
我在下面有这个输出:
_list is a List<String>
4
3
_segments is a List<String>
5
Unsupported operation: removeLast
5
我不明白,我错过了什么?
显然按如下方式包装列表可以解决问题
final _segments = [...Uri.parse(_routeInfo.location).pathSegments];
import 'package:flutter/material.dart';
void main() {
final _list = ['1', '2', '3', '4'];
print('_list is a ${_list.runtimeType}');
print('${_list.last}');
try {
_list.removeLast();
} catch (e) {
print(e);
}
print('${_list.last}');
final _routeInfo = RouteInformation(location: '/user/info/5');
- final _segments = Uri.parse(_routeInfo.location).pathSegments;
+ final _segments = [...Uri.parse(_routeInfo.location).pathSegments];
print('_segments is a ${_segments.runtimeType}');
print('${_segments.last}');
try {
_segments.removeLast();
} catch (e) {
print(e);
}
print('${_segments.last}');
}