Bharat Chhabra
Bharat Chhabra

Reputation: 2026

Flutter 'owner!._debugCurrentBuildTarget == this': is not true. #87

I am getting the error when I try to load the pdf from url. It shows the number of pages of pdf but then it crashes.

Using plugin - https://pub.dev/packages/advance_pdf_viewer

════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (28488): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (28488): The following assertion was thrown while rebuilding dirty elements:
I/flutter (28488): 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4371 pos 14:
I/flutter (28488): 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (28488):
I/flutter (28488): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (28488): more information in this error message to help you determine and fix the underlying cause.
I/flutter (28488): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (28488):   https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (28488):
I/flutter (28488): The relevant error-causing widget was:
I/flutter (28488):   PDFViewer
I/flutter (28488):   
lib/…/panSignatureScreens/pan_screen.dart:156
I/flutter (28488):
I/flutter (28488): When the exception was thrown, this was the stack:
I/flutter (28488): #2      Element.rebuild.<anonymous closure> 
package:flutter/…/widgets/framework.dart:4371
I/flutter (28488): #3      Element.rebuild 
package:flutter/…/widgets/framework.dart:4374
I/flutter (28488): #4      BuildOwner.buildScope 
package:flutter/…/widgets/framework.dart:2777
I/flutter (28488): #5      WidgetsBinding.drawFrame 
package:flutter/…/widgets/binding.dart:906
I/flutter (28488): #6      RendererBinding._handlePersistentFrameCallback 
package:flutter/…/rendering/binding.dart:309
I/flutter (28488): #7      SchedulerBinding._invokeFrameCallback 
package:flutter/…/scheduler/binding.dart:1117
I/flutter (28488): #8      SchedulerBinding.handleDrawFrame 
package:flutter/…/scheduler/binding.dart:1055
I/flutter (28488): #9      SchedulerBinding._handleDrawFrame 
package:flutter/…/scheduler/binding.dart:971
I/flutter (28488): #13     _invoke  (dart:ui/hooks.dart:251:10))
I/flutter (28488): #14     _drawFrame  (dart:ui/hooks.dart:209:3))

my usage - inside a Column(children:[])

new Expanded(
                  child: Container(
                    child: PDFViewer(
                      document: document,
                    ),
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.black),
                    ),
                  ),
                ),

Upvotes: 32

Views: 40921

Answers (10)

Antonin GAVREL
Antonin GAVREL

Reputation: 11219

In my case I had changed a stateless widget to stateful which had a parameter named as "widget". While perfectly "legal", if misused it would call itself in a recursive way.

class MyScaffold extends StatefulWidget { // formerly Stateless
  final Widget widget; // naming it 'widget' was a time consuming mistake.

  const MyScaffold({super.key, required this.widget});

  @override
  State<MyScaffold> createState() => _MyScaffoldState();
}

class _MyScaffoldState extends State<MyScaffold> {
  ...
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: widget // forgot to rename it to 'widget.widget', here it's like attempting to divide by 0 -> create an infinite stack of widget as the body of the Scaffold widget is itself.
   ...
}

Once I found the bug I actually changed the widget variable name to "body" and will never use the "widget" word as a variable name again. The code would compile fine but the issue was really tricky to debug.

Upvotes: 0

Ahmed M. Hassan
Ahmed M. Hassan

Reputation: 1286

My issue: I had two classes with the same name. Removing one of them fixed the issue for me ✅

Upvotes: 2

Muhammad Rameez
Muhammad Rameez

Reputation: 81

I solved this issue by changing the name of one of my Stateless widget.

Be Very Careful while making the name of your widget, If you make any two widgets with the same name unconsciously then you will get this type of error.

Make sure to assign the name of widget that are relevant to it so that you will not get this type of error.

Upvotes: 1

gruvw
gruvw

Reputation: 948

If you are using Hive (https://pub.dev/packages/hive), it could come from the fact that you used the wrong type T when calling await Hive.openBox<T>("boxName").

(Took me so long to figure out, so hopefully this can save you some time, maybe even to future me lol)

Upvotes: 3

Bhavesh Audichya
Bhavesh Audichya

Reputation: 151

If still anyone getting this error I solved mine by adding
WidgetsFlutterBinding.ensureInitialized() before runApp function.

void main() {
     WidgetsFlutterBinding.ensureInitialized();
     runApp(const App());
}

Upvotes: 1

Mateusz Pietras
Mateusz Pietras

Reputation: 109

For me that was happening while I had an widget instead of sliver in CustomScrollView

Upvotes: 8

Rohit Daftari
Rohit Daftari

Reputation: 51

Yes! There is a conflict with advance_pdf_viewer only if you are using Firebase Cloud Messaging Service's Background Message handler.

For example if you have something like :

Future<void> initNotifs() async {
    FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
      print("onMessage: $message");
    });
    final FirebaseMessaging messaging;
    messaging = FirebaseMessaging.instance;
    await messaging.getToken().then((c) => print(c));
    await messaging
        .subscribeToTopic('general')
        .then((value) => print('subscribed to general'));
     FirebaseMessaging.onBackgroundMessage(
         PushNotificationsHandler.firebaseMessagingBackgroundHandler);
  }

Comment the below code and/Or config the notifications in Locators/Some other View:

FirebaseMessaging.onBackgroundMessage(
             PushNotificationsHandler.firebaseMessagingBackgroundHandler);

Ps: PushNotificationsHandler is Another Class.

Upvotes: 0

David
David

Reputation: 366

Are you using FirebaseMessaging? If so, it could be a conflict of advance_pdf_viewer with onBackgroundMessage of FirebaseMessaging.

Upvotes: 0

Ishan Hettiarachchi
Ishan Hettiarachchi

Reputation: 1704

I had the same issue and the problem was I had called the class inside the same class. See the body: below. Replacing it with another widget fixed the issue.

class DicePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
                   home: Scaffold(
                   backgroundColor: Colors.red,
                   appBar: AppBar(
                             title: Text('Dicee'),
                             backgroundColor: Colors.red,
                           ),
                    body: DicePage(),
      ),
    );
  }
}

Upvotes: 34

Nicklas Bocksberger
Nicklas Bocksberger

Reputation: 121

I just had a similiar error:

════════ Exception caught by widgets library ═══════════════════════════════════
The following StackOverflowError was thrown building Container:
Stack Overflow

The relevant error-causing widget was
Container
lib/widgets/profile_wiget.dart:9
When the exception was thrown, this was the stack
#0      new _HashMap (dart:collection-patch/collection_patch.dart)
#1      new HashMap (dart:collection-patch/collection_patch.dart:33:22)
#2      RegExp._cache (dart:core-patch/regexp_patch.dart:109:11)
#3      RegExp._cache (dart:core-patch/regexp_patch.dart)
#4      new RegExp (dart:core-patch/regexp_patch.dart:17:31)
...
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4371 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
The relevant error-causing widget was
ProfileWidget
lib/widgets/profile_wiget.dart:10
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4371 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
The relevant error-causing widget was
Container
lib/widgets/profile_wiget.dart:9
════════════════════════════════════════════════════════════════════════════════

It might be another reason since I got a stackoverflow, but my answer will hopefully help others who search this error message.
My problem was that I made a widget returning an instance of itself (I named two classes the same):

class ProfileWidget extends StatelessWidget {
  final Profile profile = Profile(name: 'Someone');
  @override
  Widget build(BuildContext context) {
    return Container(
      child: (profile.isAnon) ? AnonProfileWidget() : ProfileWidget(), 
    );
  }
}

I solved it by simply renaming one class.

Upvotes: 11

Related Questions