Keeps showing Undefined name 'Stripe' while trying to add publishable key

Viewed 24

I'm adding Stripe payment to an app I'm building. I've added the Stripe package, imported it and made sure all the requirements are met. But when I try to add publishable key using Stripe.publishableKey, it's showing: Undefined name 'Stripe'. This is the the code in my main.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
...
import 'package:flutter_stripe/flutter_stripe.dart'; //.....stripe package import....


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Stripe.publishableKey = publishableKey;  //.....Undefined name 'Stripe'.......
  await Firebase.initializeApp();
}

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => ThemeProvider()),
        ChangeNotifierProvider(create: (_) => ApplicationState()),
      ],
      child: Consumer<ThemeProvider>(
        builder: (context, _themeProvider, snapshot) {
          return MaterialApp(
            title: 'Title',
            debugShowCheckedModeBanner: false,
            theme: themeLight(context),
            darkTheme: themeDark(context),
            themeMode: (_themeProvider.isDarkTheme == true)
                ? ThemeMode.dark
                : ThemeMode.light,
            initialRoute: '/',
            routes: {
              '/': (context) => const SplashScreen(),
              '/on-boarding': (context) => const OnBoardingScreen(),
              '/all-login-options': (context) =>
                  const SocialNetworkSignInScreen(),
              ...
              '/home': (context) => const Home(),
            },
          );
        },
      ),
    );
  }
}

Has anyone ever faced a similar issue before? Do you have any solutions for it? Any help will be greatly appreciated.

1 Answers

you can give the import a name and use it to access the Stripe class. this is the solution I came up with when facing the same issue

import 'package:flutter_stripe/flutter_stripe.dart' as stripe;

and then in main

stripe.Stripe.publishableKey = publishableKey;
Related