错误内容如下:
======== Exception caught by widgets library =======================================================
The following assertion was thrown building _CupertinoTextSelectionControlsToolbar(dirty, dependencies: [MediaQuery, _LocalizationsScope-[GlobalKey#1fa5b]], state: _CupertinoTextSelectionControlsToolbarState#23a43):
No CupertinoLocalizations found.
_CupertinoTextSelectionControlsToolbar widgets require CupertinoLocalizations to be provided by a Localizations widget ancestor.
The cupertino library uses Localizations to generate messages, labels, and abbreviations.
To introduce a CupertinoLocalizations, either use a CupertinoApp at the root of your application to include them automatically, or add a Localization widget with a CupertinoLocalizations delegate.
The specific widget that could not find a CupertinoLocalizations ancestor was: _CupertinoTextSelectionControlsToolbar
dirty
dependencies: [MediaQuery, _LocalizationsScope-[GlobalKey#1fa5b]]
state: _CupertinoTextSelectionControlsToolbarState#23a43
The ancestors of this widget were:
: CupertinoApp
state: _CupertinoAppState#5e387
: MyApp
: ChangeNotifierProvider
value:
: MultiProvider
...
The relevant error-causing widget was:
CupertinoApp CupertinoApp:file:///Users/jarvis/Desktop/Flutter-workspace/flutter_oa/code/oa/lib/main.dart:70:14
When the exception was thrown, this was the stack:
#0 debugCheckHasCupertinoLocalizations.
#1 debugCheckHasCupertinoLocalizations (package:flutter/src/cupertino/debug.dart:43:4)
#2 CupertinoLocalizations.of (package:flutter/src/cupertino/localizations.dart:262:12)
#3 _CupertinoTextSelectionControlsToolbarState.build (package:flutter/src/cupertino/text_selection.dart:135:73)
#4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4782:27)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4665:15)
#6 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
#7 Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
#8 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4643:5)
#9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4831:11)
#10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4638:5)
... Normal element mounting (39 frames)
#49 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14)
#50 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6333:36)
#51 Element.updateChild (package:flutter/src/widgets/framework.dart:3425:18)
#52 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5758:32)
#53 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6356:17)
#54 Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
#55 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
#56 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
#57 Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
#58 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:33)
#59 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#60 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:319:5)
#61 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1143:15)
#62 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1080:9)
#63 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:996:5)
#67 _invoke (dart:ui/hooks.dart:166:10)
#68 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#69 _drawFrame (dart:ui/hooks.dart:129:31)
(elided 3 frames from dart:async)
====================================================================================================
从上面的错误信息中的一句:
To introduce a CupertinoLocalizations, either use a CupertinoApp at the root of your application to include them automatically, or add a Localization widget with a CupertinoLocalizations delegate.
大致的意思是需要引用‘CupertinoLocalizations’这么个东西,然后用’CupertinoApp‘这个东西包裹,那么‘CupertinoLocalizations’需要放在那里呢,哎,就是放在语言代理的‘localizationsDelegates’位置下,
当然,不能直接使用‘CupertinoLocalizations’,不然也是报错,这里需要使用的是‘GlobalCupertinoLocalizations.delegate,’,这个代理,我这里是区分了iOS和android设备平台。
具体可参考下图: