Error: RenderBox was not laid out, Failed assertion: line 1940 pos 12: ‘hasSize’

This happens when ListView has no constrained height which makes it gets an infinite height, you can solve this using two solutions add shrinkWrap: true as a parameter which will tell the ListView to use a little space as possible, Wrap your ListView with a constrained height widget, such as a SizedBox or a Container, … Read more

After migrating flutter code to null-safety, mock objects not accepting `any`

When assigning the Mock object, it needs to be of the Mock object type, not the BaseClass. @GenerateMocks(MockSpec<ITransactionRepository>(as: #MockTransactionRepository), ) void main() { …. ITransactionRepository baseObject = MockTransactionRepository(); // wrong MockTransactionRepository mockObject = MockTransactionRepository(); // right when(baseObject.method(any)); // results in compile error when(mockObject.method(any)); // OK … } Source: https://github.com/dart-lang/mockito/issues/364

How to Make Two Floating Action Button in Flutter?

floatingActionButton property on Scaffold widget do not need to take FloatingActionButton widget necessarily. It can also take Column or Row widgets. Below, I’m sharing my Scaffold widget example with two floating action buttons on top of each other. return Scaffold( appBar: AppBar( title: Text(“”), ), body: SingleChildScrollView(/*…*/), floatingActionButton: Column( mainAxisAlignment: MainAxisAlignment.end, children: [ FloatingActionButton( child: … Read more

How to test Flutter widgets on different screen sizes?

You can specify custom surface size by using WidgetTester The following code will run a test with a screen size of 42×42 import ‘package:flutter/widgets.dart’; import ‘package:flutter_test/flutter_test.dart’; void main() { testWidgets(“foo”, (tester) async { tester.binding.window.physicalSizeTestValue = Size(42, 42); // resets the screen to its original size after the test end addTearDown(tester.binding.window.clearPhysicalSizeTestValue); // TODO: do something }); … Read more

Bad state: Mock method was not called within `when()`. Was a real method called?

This issue may happen when you implement a method you want to mock instead of letting Mockito do that. This code below will return Bad state: Mock method was not called within when(). Was a real method called?: class MockFirebaseAuth extends Mock implements FirebaseAuth { FirebaseUser _currentUser; MockFirebaseAuth(this._currentUser); // This method causes the issue. Future<FirebaseUser> … Read more

Is it possible to access children/parent widgets from a given widget, in a Flutter test?

Yes, you can use find.descendant and Element.ancestorWidgetOfExactType. Examples: // Finds a RichText widget that a descendant (child/grand-child/etc) of a // tab widget with text “Tab 1” find.descendant(of: find.text(‘Tab 1’), matching: find.byType(RichText)); // Finds a parent widget of type MyParentWidget. tester.element(find.byType(MyChildWidget)) .ancestorWidgetOfExactType(MyParentWidget);

How to test navigation via Navigator in Flutter

While what Danny said is correct and works, you can also create a mocked NavigatorObserver to avoid any extra boilerplate: import ‘package:mockito/mockito.dart’; class MockNavigatorObserver extends Mock implements NavigatorObserver {} That would translate to your test case as follows: void main() { testWidgets(‘Button is present and triggers navigation after tapped’, (WidgetTester tester) async { final mockObserver … Read more

How to test code that uses DateTime.now in Flutter?

As Günter said, the clock package, maintained by the Dart team, provides a very neat way to achieve this. Normal usage: import ‘package:clock/clock.dart’; void main() { // prints current date and time print(clock.now()); } Overriding the current time: import ‘package:clock/clock.dart’; void main() { withClock( Clock.fixed(DateTime(2000)), () { // always prints 2000-01-01 00:00:00. print(clock.now()); }, ); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)