Description
Problem description
I'm using ScrollablePositionedList and I'm having some issues when using Provider.of(context)
in the itemBuilder and in the main build
method. Each item on the list is built twice and the first one is out of sync with calculations in the build
method.
This behavior doesn't occur with a ListView.builder
.
Steps to reproduce
- Run the example main.dart file (https://gist.github.com/davidmartos96/7278ff982acefe7418a339464e123e40)
- Tap the Counter button
- Custom assertion fails
This is a basic example in which the value in the build
method is a copy of the counter, but it could be a computed value instead which could conflict with logic inside the itemBuilder
.
Expected behavior
The variable outerScopeCounter
from the build method should be in sync with the counter obtained from the provider in the itemBuilder.
Actual behavior
The itemBuilder is called twice and the value is not in sync the first time.
Log:
I/flutter (19893): Main build: From provider: 0
I/flutter (19893): [SPL] Item builder: From provider: 0 - From outer scope: 0
I/flutter (19893): [LVB] Item builder: From provider: 0 - From outer scope: 0
I/flutter (19893): Counter tapped
I/flutter (19893): Main build: From provider: 1
I/flutter (19893): [LVB] Item builder: From provider: 1 - From outer scope: 1
I/flutter (19893): [SPL] Item builder: From provider: 1 - From outer scope: 0
════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building:
'package:spl_provider/main.dart': Failed assertion: line 68 pos 22: 'outerScopeCounter == notifierInner.counter': is not true.
When the exception was thrown, this was the stack
#2 MyWidget.build.<anonymous closure>
package:spl_provider/main.dart:68
#3 _PositionedListState._buildItem
package:scrollable_positioned_list/src/positioned_list.dart:247
#4 _PositionedListState.build.<anonymous closure>
package:scrollable_positioned_list/src/positioned_list.dart:201
#5 SliverChildBuilderDelegate.build
package:flutter/…/widgets/sliver.dart:449
#6 SliverMultiBoxAdaptorElement._build
package:flutter/…/widgets/sliver.dart:1130
...
════════════════════════════════════════════════════════════════════════════════
I/flutter (19893): [SPL] Item builder: From provider: 1 - From outer scope: 1
Environment
Flutter 1.22
scrollable_positioned_list: ^0.1.8
provider: ^4.3.2+2