I am trying to get a CardField from the flutter_stripe package to appear on web. Simply with this code, a card field will display on ios and android. Though once I run on the app on Chrome, a blank line appears with the error, "Bad state: Source maps are not done loading".
I have the flutter_stripe and flutter_stripe_web package both installed and I cannot seem to figure this out. Any advice would be appreciated!
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
#override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
CardFieldInputDetails? _card;
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
home: Scaffold(
body: Column(
children: [
CardField(
cursorColor: Colors.black,
enablePostalCode: true,
countryCode: 'US',
postalCodeHintText: 'Enter the us postal code',
onCardChanged: (card) {
setState(() {
_card = card;
});
},
),
],
),
),
);
}
}
ios image
web image
Turns out I just needed to include Stripe.publishableKey = 'your publishable key' inside main()!
The flutter_stripe project only has limited, experimental support for the web right now. The maintainers recommend you use Stripe Checkout instead.
If you really want to get the CardField working on the web I was going to suggest you post an issue to the flutter_stripe repo, but it looks like you've already done so.
Related
A flutter newbie, I'm trying to get my app to take values from a local json file and render them on-screen.
The logic isn't waiting for the class constructor to populate the relevant string variable before rendering the app on screen.
Here's the code that illustrates my problem.
First, my main.dart file:
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'sampleDataClass.dart';
import 'package:provider/provider.dart';
String assetFilePath = 'assets/basicData.json';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (details) {
FlutterError.presentError(details);
if (kReleaseMode) exit(1);
};
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => MyAppState(),
child: const MaterialApp(
title: "Sample screen",
home: MyHomePage(),
)
);
}
}
class MyAppState extends ChangeNotifier {
SampleDataClass current = SampleDataClass(assetFilePath);
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
#override
Widget build(BuildContext context) {
var myAppState = context.watch<MyAppState>();
var myAppBasicData = myAppState.current;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.indigo,
foregroundColor: Colors.amberAccent,
title: const Text("This is the App Bar"),
elevation: 10,
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Expanded(
child: Container(
color: Colors.blueGrey,
padding: const EdgeInsets.all(10),
child: (
Text(myAppBasicData.language,
style: const TextStyle(
color: Colors.white,
))))),
]),
]
),
);
}
}
Here is my SampleDataClass.dart file:
import 'dart:core';
import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;
class SampleDataClass {
String classFilePath = "";
String language = "not populated";
String batteryName = "not populated";
SampleDataClass(filePath) {
rootBundle.loadString(filePath).then((jsonDataString) {
Map classDataMap = jsonDecode(jsonDataString);
language = classDataMap['language'];
print(language);
batteryName = classDataMap['batteryName'];
print(batteryName);
});
}
Map<String, dynamic> toJson() => {
'language': language,
'batteryName': batteryName,
};
}
And here's my pubspec.yaml file:
name: samples
description: A new Flutter project.
environment:
sdk: '>=2.18.6 <3.0.0'
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
provider: ^4.1.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
flutter:
uses-material-design: true
assets:
- assets/basicData.json
Finally, my basicData.json file looks like this:
{
"language": "English",
"batteryName": "Exercise 32"
}
The print statements in the sampleDataClass class work fine but as you can see if you run the code (from the command line using "flutter run --no-sound-null-safety"), the app renders the initial values of the variables before the class constructor assigns the json values to them.
What I expected was the class constructor to complete writing the relevant json values to the class members before handing back the class instance to the build widget in main.dart to render the app on-screen.
I realise that this is a very elementary question, but it is a key pending learning task for me!
Thank you all very much in advance!
it looks like rootBundle is an async function, you have to wait till it finishes then you notify the UI to render with new data,
How to handle this :
you should have 3 states loading state , error state , loaded state ,
create an init(path) function in SampleDataClass class that returns SampleDataClass instance.
Also, in the init() function at the beginning you have to change the state to loading state then when you get the data you set the state to loaded state, then notify listeners this will help the screen to know which state the page is in.
in the screen , call didChangeDependencies() and inside it, call your Provider => init function ( current = contecxt.read<YPUR_PROVIDER>().init(path); )
therefore the current object is being initialized, the page is showing a loader, and once the initialization is done, the provider will notify the page and it will change the loading view to the loaded view (your current view).
Another Tip::
your app will close whenever there is an error, it is not a good practice, since flutter will through non-fatal exception
FlutterError.onError = (details) {
FlutterError.presentError(details);
if (kReleaseMode) exit(1);
There is a choice of two excellent solutions to my problem with very little effort and no need for any change management (yet).
Indeed, so similar is the problem described that I probably should have found it before I posted this query here. As a newbie in this discipline, I clearly didn't conduct my search with the appropriate key words; for that I apologise!
Thanks very much to everyone for your patience.
The two solution options (which even I was able to follow) can be found here:
Calling an async method from a constructor in Dart
I'm having a problem with Flutter. More precisely, since it is not a language that I have a command of, I did something by looking at the videos. Of course, old videos etc. I've been in trouble.
First of all, I will write what I want to do and what I failed to achieve. I am pulling my website into application with webview. There is no problem here. Then I added onesignal. This also works flawlessly. But I want to get the id called playerid that onesignal defines for each device and post it to my php page. I tried many plugins for this, but I could not succeed. Then I sent it as a cookie, it still didn't work. I realized that I can send data as get in the latest webview link, but the fact that I can't pull the variable I created here also infuriated me. I'm adding the codes below and asking for help from an expert friend. If there is, you can suggest an easy way to POST and GET. Thanks everyone in advance.
In the code below, I can't get the variable that I defined as userid and printed in the link section. In the simplest way, if I do this, it can work for now.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';
void main() async {
runApp(PnlbaseApp());
}
class PnlbaseApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'PNLBASE APP',
home: PnlbaseAppPage(),
);
}
}
class PnlbaseAppPage extends StatefulWidget {
PnlbaseAppPage({Key? key}) : super(key: key);
#override
_PnlbaseAppPageState createState() => _PnlbaseAppPageState();
}
class _PnlbaseAppPageState extends State<PnlbaseAppPage> {
#override
void initState() {
super.initState();
OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
OneSignal.shared.setAppId("APP-ID");
OneSignal.shared.promptUserForPushNotificationPermission().then((accepted) {
print("Accepted permission: $accepted");
});
OneSignal.shared.getDeviceState().then((deviceState) {
print("OneSignal: device state: ${deviceState?.jsonRepresentation()}");
var userid;
userid = deviceState?.userId;
print(userid);
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
top: true,
child: WebView(
initialUrl: "https://app.pnlbase.com/login?playerid",
javascriptMode: JavascriptMode.unrestricted,
),
),
);
}
}
I have a login screen which uses phone authentication for creating account.
I have used Firebase Phone auth for login and also have stored one number for testing purpose.
But don't know how to pass the number and OTP to generate Pre Launch Report.
They are asking for Username, Username Resource ID, Password , Password Resource ID.
Where to find Resource ID for username and password fields in flutter code.
In the Google play console at the bottom of the left
Click on App content
Click on App access
Click on manage
Click on add new instructions
Add your all details here it should be test accounts
Try this :
dependencies:
flutter_runtime_env: ^0.0.4
Example:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_runtime_env/flutter_runtime_env.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isInFirebaseTestLab = false;
#override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
var result = await inFirebaseTestLab();
setState(() {
_isInFirebaseTestLab = result;
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('is in FirebaseTest Lab'),
),
body: Center(
child: Text('is in FirebaseTest Lab: $_isInFirebaseTestLab\n'),
),
),
);
}
}
I want to send sms to a particular phone number in my flutter application. I tried all flutter packages regarding this issue and github repos but I screw up and did not find any useful source.
Here's my last attempt code.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:sms/sms.dart';
void main() {
runApp(new MaterialApp(
title: "Send sms Demo",
home: new SendSms(),
));
}
class SendSms extends StatefulWidget {
#override
_SendSmsState createState() => new _SendSmsState();
}
class _SendSmsState extends State<SendSms> {
Future<Null> sendSms()async {
SmsSender smsSender = new SmsSender();
smsSender.sendSms(new SmsMessage('+*****7337544', 'test send sms')); //instead xxx... to receiver
phone
}
#override
Widget build(BuildContext context) {
return new Material(
child: new Container(
alignment: Alignment.center,
child: new FlatButton(onPressed: () => sendSms(), child: const Text("Click here to Send SMS")),
),
);
}
}
And I put this in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
I’ve recently uploaded a sample project on Github that make use of the telephony package, which request for SEND_SMS permissions and dispatch SMS.
Here is the repo
In this sample you have to fire the message with a button, but you can do it automatically if you want to.
I have an app that is managing audio calls. When a call is made to the add and the app is running in the background I need to bring the app in the foreground state. I tried to use Navigator. push but without any result.
You can use the package bringtoforeground. It's fairly in the early stages with respect to its version but it works.
iOS
But this only works on android, you have to keep in mind that iOS apps that are on the background are destroyed. you can read this do
see details here
Android
So this implementation will only work on Android.
The best thing with this package is that you can use it with Firebase Cloud Messaging (FCM) or any other for that matter.
This is their example, Bringtoforeground.bringAppToForeground(); this is the piece of code you use to bring your app to the foreground.
import 'dart:async';
import 'package:bringtoforeground/bringtoforeground.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
#override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
Timer.periodic(Duration(seconds: 10), (t) {
Bringtoforeground.bringAppToForeground(); //This is the only thing that matters
});
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
),
),
);
}
}
Install flutter_foreground_task package here is
and use FlutterForegroundTask.minimizeApp() for app to background
and use FlutterForegroundTask.launchApp() for app to foreground that's all.
I think it helps.
if you work with flutter_local_notifications package
you can add this argument to AndroidNotificationAction
from here
hope this help (: