我想使用maps_launcher包啟動谷歌地圖。
當我將下面的 TextButton 代碼復制到 MaterialApp 主頁部分中的新專案(或剝離我自己的 main.dart)時,它作業正常。 (編輯:它必須包裝在容器或腳手架中才能作業,否則我會收到相同型別的不匹配錯誤。)
但是,由于某種我不知道的原因,我不斷收到我想要的結構中的兩個錯誤之一:
場景/錯誤 A
當我使用此代碼時:
onPressed: MapsLauncher.launchQuery(businesses[index].address)
我收到此錯誤:
The argument type 'Future<bool>' can't be assigned to the parameter type 'void Function()?'.
場景/錯誤 B
當我使用此代碼時:
我收到此錯誤:
════════ Exception caught by widgets library ═══════════════════════════════════
The following _TypeError was thrown building:
type 'String' is not a subtype of type 'Widget'
When the exception was thrown, this was the stack
#0 BusinessList.build.<anonymous closure>.<anonymous closure>
#1 SliverChildBuilderDelegate.build
#2 SliverMultiBoxAdaptorElement._build
#3 SliverMultiBoxAdaptorElement.createChild.<anonymous closure>
#4 BuildOwner.buildScope
#5 SliverMultiBoxAdaptorElement.createChild
#6 RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure>
#7 RenderObject.invokeLayoutCallback.<anonymous closure>
#8 PipelineOwner._enableMutationsToDirtySubtrees
#9 RenderObject.invokeLayoutCallback
#10 RenderSliverMultiBoxAdaptor._createOrObtainChild
#11 RenderSliverMultiBoxAdaptor.addInitialChild
#12 RenderSliverList.performLayout
#13 RenderObject.layout
#14 RenderSliverEdgeInsetsPadding.performLayout
#15 RenderSliverPadding.performLayout
#16 RenderObject.layout
#17 RenderViewportBase.layoutChildSequence
#18 RenderViewport._attemptLayout
#19 RenderViewport.performLayout
#20 RenderObject.layout
#21 RenderProxyBoxMixin.performLayout
#22 RenderObject.layout
#23 RenderProxyBoxMixin.performLayout
#24 RenderObject.layout
#25 RenderProxyBoxMixin.performLayout
#26 RenderObject.layout
#27 RenderProxyBoxMixin.performLayout
#28 RenderObject.layout
#29 RenderProxyBoxMixin.performLayout
#30 RenderObject.layout
#31 RenderProxyBoxMixin.performLayout
#32 RenderObject.layout
#33 RenderProxyBoxMixin.performLayout
#34 RenderObject.layout
#35 RenderProxyBoxMixin.performLayout
#36 RenderCustomPaint.performLayout
#37 RenderObject.layout
#38 RenderProxyBoxMixin.performLayout
#39 RenderObject.layout
#40 MultiChildLayoutDelegate.layoutChild
#41 _ScaffoldLayout.performLayout
#42 MultiChildLayoutDelegate._callPerformLayout
#43 RenderCustomMultiChildLayoutBox.performLayout
#44 RenderObject._layoutWithoutResize
#45 PipelineOwner.flushLayout
#46 RendererBinding.drawFrame
#47 WidgetsBinding.drawFrame
#48 RendererBinding._handlePersistentFrameCallback
#49 SchedulerBinding._invokeFrameCallback
#50 SchedulerBinding.handleDrawFrame
#51 SchedulerBinding._handleDrawFrame
#55 _invoke (dart:ui/hooks.dart:150:10)
#56 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#57 _drawFrame (dart:ui/hooks.dart:114:31)
(elided 3 frames from dart:async)
完整代碼
這是生成上述錯誤的代碼(在粘貼程序中洗掉了包匯入):
class BusinessList extends StatelessWidget {
const BusinessList({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Business List')),
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('businesses')
.snapshots()
.map((snapshot) => snapshot.docs
.map((doc) => Business.fromJson(doc.data()))
.toList()),
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
if (snapshot.hasData) {
if (snapshot.data!.isNotEmpty) {
final businesses = snapshot.data;
return ListView.builder(
itemCount: businesses!.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Column(
children: [
ExpansionTile(
leading: const Icon(Icons.business),
title: Text(businesses[index].name),
subtitle: Text(businesses[index].category),
children: [
ListTile(
leading: const Icon(Icons.location_pin),
title: TextButton(
child: businesses[index].address,
onPressed: () => MapsLauncher.launchQuery(
businesses[index].address),
),
),
],
),
],
),
);
},
);
}
}
return const Text('error');
},
),
);
}
}
我的代碼(第 42 行)的紅色下劃線部分是:
MapsLauncher.launchQuery(businesses[index].address)
uj5u.com熱心網友回復:
我認為它launchQuery回傳 a bool,這在編譯器看來,就像你試圖給一個bool想要 a 的引數一樣void Function?
我認為這會解決它。
onPressed: () => MapsLauncher.launchQuery(businesses[index].address),
或者您可能需要執行此操作,具體取決于launchQuery作業方式:
onPressed: () async => await MapsLauncher.launchQuery(businesses[index].address),
這解決了型別不匹配的問題,因為添加() => 告訴編譯器您正在為其提供一個函式,onPressed以便在按下按鈕時執行。
免責宣告:這是未經測驗的代碼
uj5u.com熱心網友回復:
好吧,我很高興但很失望地報告說,這是一個初學者的錯誤產生的錯誤。
問題實際上不在于 onPressed,而在于需要小部件包裝字串的子級。
這是一個字串:
child: businesses[index].address
所以我所要做的就是把它包裝成這樣:
child: Text(businesses[index].address)
當錯誤看起來像是在其他地方而不是實際位置時,這真是令人沮喪。我希望這有一天能幫助某人...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/400876.html
