Logo

dev-resources.site

for different kinds of informations.

How to work javascript file in flutter web ?

Published at
3/8/2023
Categories
flutter
flutterdev
javascript
flutterweb
Author
esmaeilahmadipour
Author
17 person written this
esmaeilahmadipour
open
How to work javascript file in flutter web ?

One of the capabilities of using flutter for development is creating pwa output. Can we change the final web output (html file) with the commands we wrote in dart? Can we add a tag (like meta or p, …) to the html file with dart commands? You can see how to do this in the following article.

To start, use the default project (counter app) in flutter!

At the top of the main file we add this line:



import 'dart: js' as dartJsFile;



Enter fullscreen mode Exit fullscreen mode

In setState from the _incrementCounter method we add this line:



dartJsFile.context.callMethod ('insertTag');



Enter fullscreen mode Exit fullscreen mode

Now we need to write the JavaScript file. To do this, we need to write the following command in the app terminal:



flutter create.



Enter fullscreen mode Exit fullscreen mode

Add your JavaScript file to the project as shown below:

In the build> web folder ..

In my example , I have to write a method called (insertTag) in my js file that adds new tags inside a tag called (seo). In the file (app .js) I created, I write the method (insertTag):



function insertTag () {
var tag = document.createElement ("meta");
tag.setAttribute ("name", "custom name");
tag.setAttribute ("content", "custom content");
var element = document.getElementById ("seo");
element.appendChild (tag);
}


Enter fullscreen mode Exit fullscreen mode

And in the head of the index .html file, I call it.



<head> <script src = "app.js"> </script> </head>


Enter fullscreen mode Exit fullscreen mode

And inside the body, I define a tag called seo.



<body> <div id = "seo"> </div>



Enter fullscreen mode Exit fullscreen mode

Now, after performing deploy operations on the server or localhost, you can add a new tag to your file (index.html) each time you press the (+) button.

now worked javascript file in flutter web

flutterdev Article's
30 articles in total
Favicon
What Happens When You Apply for, loop, reduce, forEach, and map for an Empty List in Dart?
Favicon
Discover the Best Flutter Widgets to Enhance Your Mobile Apps
Favicon
Helpful Tips For Business Enterprises While Hiring Flutter Developers In 2024
Favicon
Fetching API's with Cubits in Flutter
Favicon
Shorebird x Flutter: Some Things You Should Know.
Favicon
Master Flutter Development with these Must-Have Widgets! 💪🚀
Favicon
How to Accelerate Flutter Development: Tips for Beginners
Favicon
Creating a Custom Curved Navigation Bar in Flutter
Favicon
Beyond Composability: Using Uniform as an Internal Tool
Favicon
Unlocking the Power of Generics in Dart: Using List, Set, Map, and Queue Collections
Favicon
Understanding the Basics of Mockito in Flutter(Part1)
Favicon
Unlocking the Power of Generics in Dart: Using List, Set, Map, and Queue Collections
Favicon
The Challenge of Using the Same Code Base for Android and Web: A Conceptual Overview
Favicon
How to work javascript file in flutter web ?
Favicon
First post
Favicon
Flutter Entegrasyon Testi | Bölüm 1
Favicon
Flutter Overlay loader
Favicon
Flutter App Development Tutorial - Blog Series
Favicon
Flutter Linter Kuralları Bölüm 3: Pub Kuralları 💫 🌌 ✨
Favicon
Learn about ConstrainedBox Widget in Flutter in 1 minute!
Favicon
Flutter Linter Kuralları Bölüm 2: Stil Kuralları 💫 🌌 ✨
Favicon
Flutter Ecommerce app 🛒
Favicon
Flutter Linter Kuralları Bölüm 1: Hata Kuralları 💫 🌌 ✨
Favicon
Flutter Tutorial to Build Image Editor App
Favicon
Flutter: Best Practices 💫 🌌 ✨
Favicon
Firebase for Flutter in DartPad
Favicon
Criando um cartão de crédito estilo Glassmorphism(vidro fosco)
Favicon
How to Remove White Splash Screen from Flutter App?
Favicon
Flutter Widget In Detail: MaterialApp
Favicon
Hack the camera

Featured ones: