Reputation: 1808
I am trying to use the Share package provided by the Flutter team. I tried implementing the package and when that didn't work I tried straight up copying and pasting their code, but got an error just the same. Unfortunately the error is not very helpful and the description of the error says "we need to explain this better". Any ideas?
My Code:
FlatButton(
child: Text(
'Share',
style: TextStyle(
color: Color.fromRGBO(245, 93, 62, 1.0)),
),
color: Colors.grey[100],
onPressed: () {
final RenderBox box = context.findRenderObject();
Share.share('Hello this is a test',
sharePositionOrigin:
box.localToGlobal(Offset.zero) &
box.size);
},
),
The Error:
flutter: The following assertion was thrown while handling a gesture:
flutter: type 'RenderSliverList' is not a subtype of type 'RenderBox'
flutter:
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub:
flutter: https://github.com/flutter/flutter/issues/new?template=BUG.md
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0 _EverythingState._buildEventCards.<anonymous closure> (package:loopt_in/widgets/everything.dart:175:43)
flutter: #1 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
flutter: #2 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
flutter: #3 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
flutter: #4 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
flutter: #5 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
flutter: #6 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
flutter: #7 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:184:20)
flutter: #8 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22)
flutter: #9 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7)
flutter: #10 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
flutter: #11 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
flutter: #12 _invoke1 (dart:ui/hooks.dart:165:13)
flutter: #13 _dispatchPointerDataPacket (dart:ui/hooks.dart:119:5)
flutter: Handler: onTap
flutter: Recognizer:
flutter: TapGestureRecognizer#ecc56(debugOwner: GestureDetector, state: ready, won arena, finalPosition:
flutter: Offset(196.0, 747.5), sent tap down)
Update
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:share/share.dart';
class Everything extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _EverythingState();
}
}
class _EverythingState extends State<Everything> {
Widget _buildEventCards(BuildContext context, DocumentSnapshot document) {
var width = MediaQuery.of(context).size.width;
return Container(
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 14.0),
child: Card(
elevation: 0.0,
child: Column(
children: <Widget>[
Image.asset(
document['image'],
fit: BoxFit.cover,
),
Container(
padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 20.0),
child: Column(
children: <Widget>[
SizedBox(
height: 15.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
// Icon(document['icon']),
],
),
],
),
SizedBox(
height: 10.0,
),
Wrap(
direction: Axis.horizontal,
alignment: WrapAlignment.start,
runAlignment: WrapAlignment.start,
children: <Widget>[
Chip(
backgroundColor: Colors.grey[100],
label: Text(
document['date'],
),
),
SizedBox(
width: 10.0,
),
Chip(
backgroundColor: Colors.grey[100],
label: Text(document['source']),
),
SizedBox(
width: 10.0,
),
Chip(
backgroundColor: Colors.grey[100],
label: Text(
document['location'],
),
),
],
),
SizedBox(
height: 15.0,
),
Container(
width: width,
child: Text(
document['title'],
style: TextStyle(fontSize: 24.0),
),
),
SizedBox(
height: 20.0,
),
Text(document['desc']),
SizedBox(
height: 20.0,
),
Row(
children: <Widget>[
FlatButton(
child: Text(
'More',
style: TextStyle(
color: Color.fromRGBO(118, 190, 208, 1.0)),
),
color: Colors.grey[100],
onPressed: () {
print('webview clicked');
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => WebviewScaffold(
url: document['url'],
appBar: AppBar(
title: Text(document['source']),
backgroundColor: Color.fromRGBO(
135,
142,
136,
1.0,
),
),
),
),
);
},
),
SizedBox(
width: 15.0,
),
FlatButton(
child: Text(
'Share',
style: TextStyle(
color: Color.fromRGBO(245, 93, 62, 1.0)),
),
color: Colors.grey[100],
onPressed: () {
final RenderBox box = context.findRenderObject();
Share.share('Hello this is a test',
sharePositionOrigin:
box.localToGlobal(Offset.zero) &
box.size);
},
),
],
),
],
),
),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection('stories').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildEventCards(context, snapshot.data.documents[index]),
);
});
}
// @override
// void initState() {
// // TODO: implement initState
// super.initState();
// FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
// var bannerAd = _buildBanner()..load();
// }
}
New Error Traceback
[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:
MissingPluginException(No implementation found for method share on channel plugins.flutter.io/share)
#0 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:291:7)
<asynchronous suspension>
#1 Share.share (package:share/share.dart:44:20)
#2 ShareButton.build.<anonymous closure> (package:loopt_in/widgets/everything.dart:220:17)
#3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
#4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
#5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
#6 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
Upvotes: 1
Views: 10369
Reputation: 103
In case anyone is still having issues on this. Here is a code that works for me.
shareData(BuildContext context){
Share.share('Some text here',
subject: 'Update the coordinate!',
sharePositionOrigin: Rect.fromLTWH(left, top, width, height)
);
}
Upvotes: 3
Reputation: 6871
The issue in this line
RenderBox box = context.findRenderObject();
the right side return RenderSliverList and you assigning it to RenderBox that why there is a mismatch type.
What excactly you are trying to share? so I can help with that
Edit:
You need to extract share button into its own widget. Try the following complete code. It works.
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:share/share.dart';
class Everything extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _EverythingState();
}
}
class _EverythingState extends State<Everything> {
Widget _buildEventCards(BuildContext context, DocumentSnapshot document) {
var width = MediaQuery.of(context).size.width;
return Container(
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 14.0),
child: Card(
elevation: 0.0,
child: Column(
children: <Widget>[
Image.asset(
document['image'],
fit: BoxFit.cover,
),
Container(
padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 20.0),
child: Column(
children: <Widget>[
SizedBox(
height: 15.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
// Icon(document['icon']),
],
),
],
),
SizedBox(
height: 10.0,
),
Wrap(
direction: Axis.horizontal,
alignment: WrapAlignment.start,
runAlignment: WrapAlignment.start,
children: <Widget>[
Chip(
backgroundColor: Colors.grey[100],
label: Text(
document['date'],
),
),
SizedBox(
width: 10.0,
),
Chip(
backgroundColor: Colors.grey[100],
label: Text(document['source']),
),
SizedBox(
width: 10.0,
),
Chip(
backgroundColor: Colors.grey[100],
label: Text(
document['location'],
),
),
],
),
SizedBox(
height: 15.0,
),
Container(
width: width,
child: Text(
document['title'],
style: TextStyle(fontSize: 24.0),
),
),
SizedBox(
height: 20.0,
),
Text(document['desc']),
SizedBox(
height: 20.0,
),
Row(
children: <Widget>[
FlatButton(
child: Text(
'More',
style: TextStyle(
color: Color.fromRGBO(118, 190, 208, 1.0)),
),
color: Colors.grey[100],
onPressed: () {
print('webview clicked');
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => WebviewScaffold(
url: document['url'],
appBar: AppBar(
title: Text(document['source']),
backgroundColor: Color.fromRGBO(
135,
142,
136,
1.0,
),
),
),
),
);
},
),
SizedBox(
width: 15.0,
),
new MyButton(),
],
),
],
),
),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection('stories').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
return ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildEventCards(context, snapshot.data.documents[index]),
);
});
}
// @override
// void initState() {
// // TODO: implement initState
// super.initState();
// FirebaseAdMob.instance.initialize(appId: FirebaseAdMob.testAppId);
// var bannerAd = _buildBanner()..load();
// }
}
class MyButton extends StatelessWidget {
const MyButton({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return FlatButton(
child: Text(
'Share',
style: TextStyle(
color: Color.fromRGBO(245, 93, 62, 1.0)),
),
color: Colors.grey[100],
onPressed: () {
final RenderBox box = context.findRenderObject();
Share.share('Hello this is a test',
sharePositionOrigin:
box.localToGlobal(Offset.zero) &
box.size);
},
);
}
}
Upvotes: 6