Flutter报错:BuildContext无法赋值给String类型参数求解决

问题求助:Flutter类型错误解决
错误信息:The argument type 'BuildContext' can't be assigned to the parameter type 'String'
代码如下:
Widget recentFileButton( BuildContext context, String fileName, String fileURL, String thumbnailImageName, String fileFrom, String timestamp, String documentID, String fileNotes) => Container( margin: const EdgeInsets.only(bottom: 10), child: Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ getPreview(fileURL, fileNotes), const SizedBox(width: 10), Expanded( child: InkWell( onTap: () async { bool _isEmwatch = false; if (fileNotes == "referral letter") { var uri = Uri.parse(fileURL); var uuid = uri.queryParameters['uuid']; var result = await DocumentRepository() .doctorGetPatientReferralByUID(uuid!); if (result.responseMap.clinicUID == "") { _isEmwatch = true; } if (result.responseMap.clinicUID == "") { _isEmwatch = true; } // generate and preview var doc = await generateDocument(PdfPageFormat.standard, result.responseMap, '', _isEmwatch); Navigator.push( context, MaterialPageRoute( builder: (context) => PdfPreviewPage(pdfDoc: doc)), ); } else { openDocument(context, fileURL); } }, ), ], ), ), ), const SizedBox(width: 10), Align( alignment: Alignment.topRight, child: IconButton( icon: const Icon( Icons.more_vert, color: Color(0xFF021522), ), onPressed: () => showFileActionBottomSheet( context, fileName, fileURL, documentID, fileNotes), ), ) ], ), );
错误原因及解决方案
这个错误是因为你调用的函数存在参数顺序或类型不匹配,将BuildContext类型变量传给了需要String类型的参数,重点检查以下两处:
openDocument(context, fileURL)
大概率openDocument的函数定义是第一个参数接收文件URL(String类型),第二个参数才是BuildContext,你传反了参数顺序,修改为:openDocument(fileURL, context);showFileActionBottomSheet(context, fileName, fileURL, documentID, fileNotes)
检查该函数的参数定义,如果它的第一个参数并非BuildContext,而是某个String类型参数,就会触发错误。比如函数定义是showFileActionBottomSheet(String fileName, BuildContext context, ...),则需要调整参数顺序,将context放到对应位置。
另外,你的代码中InkWell的闭合标签存在语法错误(多余的), ], ), ),),需要修正后才能正常编译。
内容的提问来源于stack exchange,提问作者John Reaper
相关产品推荐
相关产品推荐

