I'm creating plugin for flutter. And face a issue from Android native.
I'm using Kotlin for Android and want to use value from platform channel from Flutter.
It's hard to explain in English. Here's a code.
fun Foo(): Any? {
methodChannel.invokeMethod(
"method",
null,
object : ErrorLogResult("tag") {
override fun success(result: Any?) {
super.success(result)
// Get result.
}
})
// return result from result callback after callback is done.
}
It can be like below in dart code.
Future<dynamic> Foo() async {
final completer = Completer();
someFunctionWithCallback((result) {
completer.complete(result);
});
return completer.future;
}
Old question, but maybe someone will come across this issue, as I had.
Yes, there is something similar: CompletableDeferred
Example pseudocode:
#Volatile
private var completableMeetingService = CompletableDeferred<MeetingService>()
// we want to join meeting, but have to wait for initialization
completableMeetingService.await().joinMeetingWithParams
// completing deffered in some listener
completableMeetingService.complete(zoomSDK.meetingService)
Related
I have video stream in mjpeg format, so I have read and implmented a lot of ways to work with it, but anything of these ways don't meet my requirments (also I was using webView packages, but now we should implement changing screen oreintation feature and webView dosen't have such thing, so I am using Hero animation to make it works, but in that case my urls with video stream open twice. It dosen't work because it has limitation: only one user can watch stream, in other cases stream will be closed).
So I have found the way to play mjepeg video using Kotlin, and read bout MethodChannel in Flutter, I implemented almost everything as it was said in docs, but it still dosen't work.
Native code:
class MainActivity: FlutterActivity() {
private val channel = "com.example.my_app"
private var view: MjpegView? = null
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler {
call, result ->
if(call.method == "Video") {
view = findViewById(R.id.mjpegid)
view!!.isAdjustHeight = true
view!!.mode1 = MjpegView.MODE_FIT_WIDTH
view!!.setUrl("http://videos/videos/LiveViewCamera")
view!!.isRecycleBitmap1 = true
result.success("it successed!")
}
else {
result.notImplemented()
}
}
}
}
in Flutter:
final channel = MethodChannel('com.example.my_app');
String videos = '';
Future getVideoFromNative() async {
videos = await channel.invokeMethod('Video');
print(videos);
print('are you hehe?');
setState(() {});
}
Got this error:
E/MethodChannel#com.example.rosysk_nano_cleaned(17621): Failed to handle method call
E/MethodChannel#com.example.rosysk_nano_cleaned(17621): java.lang.NullPointerException
When I open it in pure Kotlin app, my video and code works perfectly fine
at the moment I have a Flutter App that when I press a button it opens a Native Kotlin activity(mainActivity), once this executes I automatically open a SecondActivity that extends AppCompatActivity, the issue becomes is that I want to return a bool from the SecondActivity back to Flutter.
At the moment when the process finishes i use "finish()" to close my Native code and return to flutter successfully but I'd like to pass a result to Flutter.
I know how to do it from the mainActivity as it uses the FlutterActivity directly and can pass "result.success()", but from the different activity I can't find a solution.
I was wondering if somebody can tell me how to pass the bool from the secondActivity to Flutter.
Thank you very much everyone in advance!
It works exactly the same. Store Result in class property and call one of its methods from onActivityResult when the second activity finishes. Something like this:
class YourPlugin ... {
...
private var pendingResult: Result? = null
...
override fun onMethodCall(#NonNull call: MethodCall, #NonNull result: Result) {
...
pendingResult = result
activity?.startActivityForResult(...)
...
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?): Boolean {
...
pendingResult?.success(<result>)
...
return false
}
I am only familiar with HTML/CSS/JS, and basics of dart/flutter
Developer Level: Beginner
Project type & language: I am developing a notes app for myself, using flutter.
My aim is to save my note, as soon as I update the text... for which I need to use a dart Function to run on every 'text-change' event..
How do I use the Text-Changes event of Quill Editor to detect changes in the Content
THE EQUIVALENT OF THIS IN JAVASCRIPT IS GIVEN BELOW , BUT I DON'T KNOW HOW TO DO IT DART & FLUTTER.
quill.on('text-change', function(delta, oldDelta, source) {
if (source == 'api') {
console.log("An API call triggered this change.");
} else if (source == 'user') {
console.log("A user action triggered this change.");
}
});
You can listen to quill document changes stream and handle it accordingly.
_controller.document.changes.listen((event) {
print(event.item1); //Delta
print(event.item2); //Delta
print(event.item3); //ChangeSource
});
I am also run into this issue. After some hours of research, I found a solution. You habe to add a listener to your QuillController, which will be called on each editor event, like pressed keys or toolbar actions.
Use the initState() method in your State class for adding a listener.
class _TextEditorState extends State<TextEditor> {
final QuillController _controller = QuillController.basic();
#override
void initState() {
_controller.addListener(() {
print('Here I am, rock me like a hurricane!!!');
});
super.initState();
}
#override
Widget build(BuildContext context) {
// Build your Widget with QuillToolbar and QuillEditor here...
}
});
Here's how it's done:
await for (final change in _quillEditorController.changes) {
final oldDelta = change.toList()[0];
final changeDelta = change.toList()[1];
final changeSource = change.toList()[2];
if (changeSource == ChangeSource.REMOTE) {
console.log("An API call triggered this change.");
} else if (changeSource == ChangeSource.LOCAL) {
console.log("A user action triggered this change.");
}
}
Note that flutter-quill's api doesn't exactly match quill.js. And since the documentation is lacking, your best hope in understanding what's available is by digging into the code-base using your editor (eg. using "go to definition").
I am now using this functon to fetch new music in flutter:
class QuietPlayQueueInterceptor extends PlayQueueInterceptor {
#override
Future<List<MusicMetadata>> fetchMoreMusic(BackgroundPlayQueue queue, PlayMode playMode) async {
if (queue.queueId == kFmPlayQueueId) {
final musics = await (neteaseRepository!.getPersonalFmMusicsFromQueue() as FutureOr<List<Music>>);
final musicListExt= musics.toMetadataList();
return musicListExt;
}
return super.fetchMoreMusic(queue, playMode);
}
}
and this is the function getPersonalFmMusicsFromQueue define:
Future<List<Music>?> getPersonalFmMusicsFromQueue() async {
if(fmPlayQueue.isEmpty){
return getPersonalFmMusics();
}else{
final Music music = fmPlayQueue.first;
final List<Music> musics = List.empty(growable: true);
musics.add(music);
return Future.value(musics);
}
}
what makes me confusing is that the getPersonalFmMusicsFromQueue function did not return any result. I make a breakpoint on the line final musicListExt= musics.toMetadataList(); but did not hited. The console is no error output. where am I doing wrong? what should I do to fix this problem?
getPersonalFmMusics looks asynchronous? Perhaps you're not awaiting
Future<List<Music>?> getPersonalFmMusicsFromQueue() async {
if(fmPlayQueue.isEmpty){
return await getPersonalFmMusics();
}
// ...
I would also advise against casting unless you're sure you need it. Instead, have the return type of getPersonalFmMusicsFromQueue return a FutureOr
(neteaseRepository!.getPersonalFmMusicsFromQueue() as FutureOr<List<Music>>); // Remove FutureOr<List<Music>>
// and make the function signature instead look like this:
FutureOr<List<Music>> getPersonalFmMusicsFromQueue(); // Force unwrapping with a `!` but also throwing proper exceptions when null.
The reason being is that casting usually hides errors the compiler would otherwise be warning you about.
Another idea I have if the above isn't the issue is the .first call.
final Music music = fmPlayQueue.first;
If this is a first getter on a stream then that need to be awaited and it likely isn't working because it's just indefinitely waiting for the stream to output a value.
I know these libraries flutter_isolate and isolate_handler exist that support doing these but I couldn't find any method to call platform specific codes from inside of them. Can someone show any example of how it's done ?
My answer might not answer to your question directly but I was facing the same situation.
I was working on this to run a computing intensive task avoiding UI lagging. After a long research, no, you cannot run Platform from other isolates rather than the main one.
Instead of making and running the native code on another isolate, let's make the native code run in background instead with TaskQueue.
Channels and platform threading
The example code in the above link is using onAttachedToEngine, you can check it out. However, I was using is configureFlutterEngine so I have to figure out a little bit until I found a solution that I need binaryMessenger to make it working. Luckily it can called from flutterEngine too!
This is the example code of when using configureFlutterEngine
class MainActivity: FlutterFragmentActivity() {
val MOBILE_SDK_CHANNEL = "com.example.app/flutter"
override fun configureFlutterEngine(#NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
super.configureFlutterEngine(flutterEngine)
val taskQueue = flutterEngine.dartExecutor.binaryMessenger.makeBackgroundTaskQueue()
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, MOBILE_SDK_CHANNEL,
StandardMethodCodec.INSTANCE, taskQueue).setMethodCallHandler { call, result ->
// TODO : Do implementation here
when (call.method) {
"example_method" -> {
// success
// result.success(data)
}
else -> result.notImplemented()
}
}
}
}
With taskQueue the native code will be ready to run in background.
Future<void> getData() async {
if (Platform.isAndroid) {
const platform = MethodChannel("com.example.app/flutter");
var data = await platform.invokeMethod("example_method");
// data is ready to use and non-blocked the UI thread
return data
}
}
The native code now runs in non-blocking manner, no more UI lag. :)