Flutter 'owner!._debugCurrentBuildTarget == this':不是真的。第87章

问题描述 投票:0回答:10

当我尝试从 url 加载 pdf 时收到错误。它显示了 pdf 页数,但随后崩溃了。

使用插件 - 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))

我的用法 - 在 Column(children:[]) 内

new Expanded(
                  child: Container(
                    child: PDFViewer(
                      document: document,
                    ),
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.black),
                    ),
                  ),
                ),
flutter dart flutter-layout pdf-viewer
10个回答
30
投票

我遇到了同样的问题,问题是我在同一个班级中调用了该班级。请参阅正文:如下。用另一个小部件替换它解决了这个问题。

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(),
      ),
    );
  }
}

10
投票

我刚刚遇到了类似的错误:

════════ 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
════════════════════════════════════════════════════════════════════════════════

这可能是另一个原因,因为我得到了 stackoverflow,但我的回答希望能帮助其他搜索此错误消息的人。
我的问题是我制作了一个返回其自身实例的小部件(我将两个类命名为相同):

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

我通过简单地重命名一个类来解决这个问题。


8
投票

对我来说,这发生在我有一个小部件而不是

CustomScrollView

中的条子时

2
投票

如果您使用 Hive (https://pub.dev/packages/hive),则可能是因为您在调用

T
时使用了错误的类型
await Hive.openBox<T>("boxName")

(我花了很长时间才弄清楚,所以希望这可以节省你一些时间,甚至可能为未来的我节省一些时间,哈哈)


2
投票

我的问题:我有两个同名的课程。删除其中一个解决了我的问题 ✅


1
投票

如果仍然有人遇到此错误,我通过添加解决了我的问题
WidgetsFlutterBinding.ensureInitialized() 在 runApp 函数之前。

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

1
投票

我通过更改我的一个无状态小部件的名称解决了这个问题。

在创建小部件的名称时要非常小心,如果您无意中创建了任何两个具有相同名称的小部件,那么您将收到此类错误。

确保分配与其相关的小部件的名称,这样您就不会收到此类错误。


0
投票

您在使用 FirebaseMessaging 吗?如果是这样,则可能是 advance_pdf_viewer 与 FirebaseMessaging 的 onBackgroundMessage 发生冲突。


0
投票

是的!仅当您使用 Firebase 云消息服务的后台消息处理程序时,才会与

advance_pdf_viewer
发生冲突。

例如,如果您有类似的内容:

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);
  }

注释以下代码和/或在定位器/其他一些视图中配置通知:

FirebaseMessaging.onBackgroundMessage(
             PushNotificationsHandler.firebaseMessagingBackgroundHandler);

Ps:

PushNotificationsHandler
是另一个班级。


0
投票

就我而言,我已将无状态小部件更改为有状态小部件,其中有一个名为“小部件”的参数。虽然完全“合法”,但如果滥用,它会以递归方式调用自身。

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.
   ...
}

一旦发现错误,我实际上将小部件变量名称更改为“body”,并且再也不会使用“widget”一词作为变量名称。代码可以很好地编译,但问题确实很难调试。

© www.soinside.com 2019 - 2024. All rights reserved.