{
    "componentChunkName": "component---node-modules-rocketseat-gatsby-theme-docs-core-src-templates-docs-query-js",
    "path": "/docs/flutter/bloc-example/",
    "result": {"data":{"mdx":{"id":"4af95926-f32d-5c21-bc04-c73b80c54efa","excerpt":"Let's get right into the bloc example. We also have a Flutter bloc plugin (which is coming soon) Initialize and Fetch your Subscriptions In this example, we use…","fields":{"slug":"/docs/flutter/bloc-example/"},"frontmatter":{"title":"Flutter BLoC Plugin Example","description":"A complete bloc example on how to use LinkFive with the Flutter bloc plugin","image":null,"disableTableOfContents":null},"body":"var _excluded = [\"components\"];\nfunction _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n/* @jsxRuntime classic */\n/* @jsx mdx */\n\nvar _frontmatter = {\n  \"title\": \"Flutter BLoC Plugin Example\",\n  \"description\": \"A complete bloc example on how to use LinkFive with the Flutter bloc plugin\"\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n    props = _objectWithoutProperties(_ref, _excluded);\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"Let's get right into the bloc example.\"), mdx(\"blockquote\", null, mdx(\"p\", {\n    parentName: \"blockquote\"\n  }, \"We also have a Flutter bloc plugin (which is coming soon)\")), mdx(\"h2\", {\n    \"id\": \"initialize-and-fetch-your-subscriptions\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#initialize-and-fetch-your-subscriptions\",\n    \"aria-label\": \"initialize and fetch your subscriptions permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Initialize and Fetch your Subscriptions\"), mdx(\"p\", null, \"In this example, we use Cubits that emits the state to their listeners.\"), mdx(\"p\", null, \"Please initialize before you provide the Cubits (Maybe in your main-method)\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-dart\"\n  }, \"LinkFivePurchases.init(\\\"API_KEY\\\");\\n\")), mdx(\"p\", null, \"And to fetch all available subscriptions (maybe when the user enters your paywall), call the fetchSubscriptions method.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-dart\"\n  }, \"LinkFivePurchases.fetchProducts();\\n\")), mdx(\"h2\", {\n    \"id\": \"create-a-new-state-class\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#create-a-new-state-class\",\n    \"aria-label\": \"create a new state class permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Create a new State Class\"), mdx(\"p\", null, \"The state class will hold the data and notify all consumer that something has changed with a given state. There are 2 states:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"strong\", {\n    parentName: \"li\"\n  }, \"Init State\"), \": LinkFiveProductsInitState - when the plugin is not yet initialized or has no data yet.\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"strong\", {\n    parentName: \"li\"\n  }, \"Loaded State\"), \": LinkFiveProductsLoadedState - when data arrived and your app can show subscription purchase buttons.\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-dart\"\n  }, \"abstract class LinkFiveProductsState extends Equatable {\\n  @override\\n  List<Object> get props => [];\\n}\\n\\nclass LinkFiveProductsInitState extends LinkFiveProductsState {}\\n\\nclass LinkFiveProductsLoadedState extends LinkFiveProductsState {\\n  final LinkFiveSubscriptionData products;\\n  LinkFiveProductsLoadedState(this.products);\\n\\n  @override\\n  List<Object> get props => products.productDetailList;\\n}\\n\")), mdx(\"h2\", {\n    \"id\": \"create-a-new-cubit-class\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#create-a-new-cubit-class\",\n    \"aria-label\": \"create a new cubit class permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Create a new Cubit class\"), mdx(\"p\", null, \"We're using a Cubit to notify all listeners that something has changed with the state we've just created.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-dart\"\n  }, \"class LinkFiveProductsCubit extends Cubit<LinkFiveProductsState> {\\n\\n}\\n\")), mdx(\"p\", null, \"Listen for subscription changes in it's constructor.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-dart\"\n  }, \"LinkFiveProductsCubit() : super(LinkFiveProductsInitState()){\\n  _stream = LinkFivePurchases\\n      .products\\n      .listen((LinkFiveProducts products) {\\n        emit(LinkFiveProductsLoadedState(data));\\n      });\\n}\\n\")), mdx(\"p\", null, \"Please cancel the stream when the cubit is closed:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-dart\"\n  }, \"@override\\nFuture<void> close() {\\n  _stream?.cancel();\\n  return super.close();\\n}\\n\")), mdx(\"h2\", {\n    \"id\": \"add-the-cubit-to-your-other-blocproviders\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#add-the-cubit-to-your-other-blocproviders\",\n    \"aria-label\": \"add the cubit to your other blocproviders permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Add the Cubit to your other BlocProviders\"), mdx(\"p\", null, \"In your main method or wherever you want to add the cubit to the widget tree, add the cubit like any other bloc or cubit.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-dart\"\n  }, \"MultiBlocProvider(providers: [\\n  BlocProvider<LinkFiveProductsCubit>(\\n      create: (context) => LinkFiveProductsCubit()),\\n  // ...\\n], child: YourAppWidgets());\\n\")), mdx(\"h2\", {\n    \"id\": \"consume-the-cubit\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#consume-the-cubit\",\n    \"aria-label\": \"consume the cubit permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Consume the Cubit\"), mdx(\"p\", null, \"In your build method, right before you want to offer the subscriptions, you can consume the cubit using a BlocBuilder< LinkFiveProductsCubit,  LinkFiveProductsState>\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-dart\"\n  }, \"BlocBuilder<LinkFiveProductsCubit, LinkFiveProductsState>(\\n    builder: (context, state) {\\n  if (state is LinkFiveProductsLoadedState) {\\n    if (state.products.productDetailList.length > 0) {\\n      return Container(\\n        child: Column(\\n            children: state.products.linkFiveSkuData\\n                .map((e) => ElevatedButton(\\n                    onPressed: () => LinkFivePurchases.purchase(e.productDetails),\\n                    child: Text(e.productDetails.title)))\\n                .toList()),\\n      );\\n    }\\n  }\\n  return Text(\\\"No products\\\");\\n}),\\n\\n\")), mdx(\"p\", null, \"When the user presses the button, LinkFivePurchases.purchase(...) will be called and handles the whole purchase flow.\"), mdx(\"p\", null, \"The verified receipt will be passed to the activeSubscription stream.\"), mdx(\"h2\", {\n    \"id\": \"active-subscription-cubit\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#active-subscription-cubit\",\n    \"aria-label\": \"active subscription cubit permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Active Subscription Cubit\"), mdx(\"p\", null, \"You can now also do the same for an Active Subscriptions Cubit.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-dart\"\n  }, \"class LinkFiveActiveProductsCubit extends Cubit<LinkFiveActiveProductsState>{\\n\\n  /// LinkFive client\\n  final LinkFivePurchasesMain linkFivePurchases = LinkFivePurchasesMain();\\n\\n  /// Stream that will be cleaned on close\\n  StreamSubscription? _stream;\\n\\n  LinkFiveActiveProductsCubit() : super(LinkFiveActiveProductsInitState()){\\n    _stream = linkFivePurchases\\n        .activeProducts\\n        .listen((LinkFiveActiveProducts activeProducts) {\\n          emit(LinkFiveActiveProductsLoadedState(data));\\n        });\\n  }\\n\\n  @override\\n  Future<void> close() {\\n    _stream?.cancel();\\n    return super.close();\\n  }\\n}\\n\\nabstract class LinkFiveActiveProductsState extends Equatable{\\n  @override\\n  List<Object> get props => [];\\n}\\n\\nclass LinkFiveActiveProductsInitState extends LinkFiveActiveProductsState {}\\n\\nclass LinkFiveActiveProductsLoadedState extends LinkFiveActiveProductsState {\\n  final LinkFiveActiveProducts activeProducts;\\n  LinkFiveActiveProductsLoadedState(this.activeProducts);\\n\\n  @override\\n  List<Object> get props => activeProducts.planList;\\n}\\n\")));\n}\n;\nMDXContent.isMDXComponent = true;","headings":[{"depth":2,"value":"Initialize and Fetch your Subscriptions"},{"depth":2,"value":"Create a new State Class"},{"depth":2,"value":"Create a new Cubit class"},{"depth":2,"value":"Add the Cubit to your other BlocProviders"},{"depth":2,"value":"Consume the Cubit"},{"depth":2,"value":"Active Subscription Cubit"}]}},"pageContext":{"slug":"/docs/flutter/bloc-example/","next":{"label":"Introduction","link":"/docs/docs/"},"repositoryEditUrl":"","repositoryProvider":""}},
    "staticQueryHashes": ["1954253342","2328931024","2501019404","973074209"]}