skip to Main Content

So I was building a flutter native plugin. I have two method (in xyzPlugin.m) which looks like this

+ (void)registerWithRegistrar:(nonnull NSObject<FlutterPluginRegistrar> *)registrar {

and

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {

Now, I want to share instance variable between both of them.

Suppose. I initialize meetingView like this inside registerWithRegistrar

 MeetingView *meetingView = [MeetingView new];

How can I use the same in handleMethodCall?

What have I tried?

@implementation xyzPlugin {
    MeetingView *_view;
}

and then in

- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
    _view = [MeetingView new];

but this gives following error

Instance variable ‘_view’ accessed in class method

2

Answers


  1. Maybe you need a global variable, so you can claim your object with a static keyword like:

    static MeetingView *_meetingView = ...
    

    then you can access this object in between class methods in Objective-C.

    Login or Signup to reply.
  2. + (instancetype)shared{
        static id obj;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            obj = [self new];
        });
        return obj;
    }
    // [MeetingView shared]...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search