| // Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| /// An example with more complicated scope |
| import 'dart:async'; |
| import 'dart:collection'; |
| |
| final libraryPublicFinal = MyTestClass(); |
| |
| final _libraryPrivateFinal = 1; |
| Object? libraryNull; |
| var libraryPublic = <String>['library', 'public', 'variable']; |
| var notAList = NotReallyAList(); |
| |
| var _libraryPrivate = ['library', 'private', 'variable']; |
| |
| var identityMap = <String, int>{}; |
| |
| var map = <Object, Object>{}; |
| |
| void staticFunction(int formal) { |
| print(formal); // Breakpoint: staticFunction |
| } |
| |
| void main() async { |
| print('Initial print from scopes app'); |
| var local = 'local in main'; |
| var intLocalInMain = 42; |
| var testClass = MyTestClass(); |
| Object? localThatsNull; |
| identityMap['a'] = 1; |
| identityMap['b'] = 2; |
| map['a'] = [1, 2, 3]; |
| map['b'] = 'something'; |
| notAList.add(7); |
| |
| String nestedFunction<T>(T parameter, Object aClass) { |
| var another = int.tryParse('$parameter'); |
| return '$local: parameter, $another'; // Breakpoint: nestedFunction |
| } |
| |
| dynamic nestedWithClosure(String banana) { |
| return () => '$local + $banana'; |
| } |
| |
| Timer.periodic(const Duration(seconds: 1), (Timer t) { |
| var ticks = t.tick; |
| // ignore: unused_local_variable, prefer_typing_uninitialized_variables |
| var closureLocal; |
| libraryPublicFinal.printCount(); |
| staticFunction(1); |
| print('ticking... $ticks (the answer is $intLocalInMain)'); |
| print(nestedFunction('$ticks ${testClass.message}', Timer)); |
| print(localThatsNull); |
| print(libraryNull); |
| var localList = libraryPublic; |
| print(localList); |
| localList.add('abc'); |
| var f = testClass.methodWithVariables(); |
| print(f('parameter')); |
| var num = '1234'.someExtensionMethod(); |
| print('$num'); |
| }); |
| |
| print(_libraryPrivateFinal); |
| print(_libraryPrivate); |
| print(nestedFunction(_libraryPrivate.first, Object)); |
| print(nestedWithClosure(_libraryPrivate.first)()); |
| } |
| |
| String libraryFunction(String arg) { |
| print('calling a library function with $arg'); |
| var concat = 'some constant plus $arg plus whatever'; |
| print(concat); |
| return concat; |
| } |
| |
| abstract class MyAbstractClass { |
| String abstractField = 'abstract-field-value'; |
| } |
| |
| class MyTestClass<T> extends MyAbstractClass { |
| final String message; |
| |
| String? notFinal; |
| |
| MyTestClass({this.message = 'world'}) { |
| myselfField = this; |
| tornOff = toString; |
| } |
| |
| String hello() => message; |
| |
| String Function(String) methodWithVariables() { |
| print('Test class is of type $T'); |
| var local = '$message + something'; |
| print(local); |
| return (String parameter) { |
| // Be sure to use a field from this, so it isn't entirely optimized away. |
| var closureLocalInsideMethod = '$message/$local/$parameter'; |
| print(closureLocalInsideMethod); |
| return closureLocalInsideMethod; // Breakpoint: nestedClosure |
| }; |
| } |
| |
| //ignore: avoid_returning_this |
| MyTestClass get myselfGetter => this; |
| |
| late final MyTestClass myselfField; |
| |
| var count = 0; |
| |
| // An easy location to add a breakpoint. |
| void printCount() { |
| print('The count is ${++count}'); |
| libraryFunction('abc'); // Breakpoint: printMethod |
| } |
| |
| final _privateField = 'a private field'; |
| |
| // ignore: unused_element |
| String privateMethod(String s) => '$s : $_privateField'; |
| |
| @override |
| String toString() => 'A test class with message $message'; |
| |
| bool equals(Object other) { |
| if (other is MyTestClass) return message == other.hello(); |
| return false; |
| } |
| |
| Function closure = someFunction; |
| |
| late final String Function() tornOff; |
| } |
| |
| Function? someFunction() => null; |
| |
| // ignore: unused_element |
| int _libraryPrivateFunction(int a, int b) => a + b; |
| |
| class NotReallyAList extends ListBase<Object?> { |
| final List<Object?> _internal; |
| |
| NotReallyAList() : _internal = []; |
| |
| @override |
| Object? operator [](x) => _internal[x]; |
| |
| @override |
| operator []=(int x, Object? y) => _internal[x] = y; |
| |
| @override |
| int get length => _internal.length; |
| |
| @override |
| set length(x) => _internal.length = x; |
| } |
| |
| extension NumberParsing on String { |
| int someExtensionMethod() { |
| var ret = int.parse(this); |
| return ret; // Breakpoint: extension |
| } |
| } |